【发布时间】:2010-03-17 15:15:11
【问题描述】:
最近我参加了一个关于一些设计模式的讲座:
已显示以下代码:
public static Singleton getInstance()
{
if (instance == null)
{
synchronized(Singleton.class) { //1
Singleton inst = instance; //2
if (inst == null)
{
synchronized(Singleton.class) { //3
inst = new Singleton(); //4
}
instance = inst; //5
}
}
}
return instance;
}
取自:Double-checked locking: Take two
我的问题与上述模式无关,而是与同步块有关:
在第 1 行和第 3 行中完成的双重同步是否有任何好处,因为同步操作是在相同的对象上完成的?
【问题讨论】:
标签: java multithreading synchronization