单例模式比较懒汉模式的优化代码

public class Singleton1 {优化

private static Singleton1 instance = null;

private Singleton1() {
}

public static Singleton1 getInstances() throws InterruptedException {
	if (instance == null) { //为了优化,当instance不为空是,直接返回实例对象
		
		synchronized (Singleton1.class) { 
			if (instance == null) {
				Thread.sleep(100);
				instance = new Singleton1();
			}
			return instance;
		}
	}
	return instance;
}

}code

相关文章
相关标签/搜索