public class Singleton {
private static Singleton instance = null;
private void Singleton()
{}
public Singleton getInstance(){
//位置1,第1次检查instance
if( null == instance){
//位置2,某一时刻可能有n个线程到达
synchronized(this)
{
//位置3,任何时间只能有1个线程到达
if(null == instance)
{
instance = new Singleton();//位置4,第2次检查instance
}
}
}
return instance;
}
}
阅读(591) | 评论(0) | 转发(0) |