JAVA设计模式之懒汉式线程安全二

public class SafeLazySingleton { //性能有缺陷

private static SafeLazySingleton mInstance;

private SafeLazySingleton(){

}

public static SafeLazySingleton getmInstance(){ //同步代码块
    synchronized (SafeLazySingleton.class){
        if(mInstance == null){
            mInstance = new SafeLazySingleton();
        }
        return mInstance;
    }
}

}
同步代码块来实现的,实际和第一种方式差不多,只是稍微有些不同通过同步代码块来控制多线程的访问和操作。想要更多资料,撩小娜哦:gzitcast

你可能感兴趣的:(java)