Singleton b = Singleton.a;
Singleton b2 = Singleton.a;
為什么我把所有的instance換成a就報錯
Singleton b2 = Singleton.a;
為什么我把所有的instance換成a就報錯
2016-04-26
覺得懶漢模式應(yīng)該再詳細(xì)點,有時多個線程時,一個判斷會出現(xiàn)等待,如果跳出去了 第二個線程來了,因為上面的線程還在等待,所以沒有創(chuàng)建,就會有沖突。比如把他設(shè)計成:
public static Single getIntece()
{
if(s==null)
{
{
synchronized(Single.class);
}
if(s==null)
{
s = new Single();
}
}
return s;
}
public static Single getIntece()
{
if(s==null)
{
{
synchronized(Single.class);
}
if(s==null)
{
s = new Single();
}
}
return s;
}
2016-04-08