我正在查看我们应用程序中的一些代码,我认为这些代码可能会遇到“双重检查锁定”的情况。我已经编写了一些与我们所做的类似的示例代码。
谁能看到这是如何经历双重检查锁定的?或者这样安全吗?
class Foo {
private Helper helper = null;
public Helper getHelper() {
Helper result;
synchronized(this) {
result = helper;
}
if (helper == null) {
synchronized(this) {
if (helper == null) {
helper = new Helper();
}
}
}
return helper;
}
}
从wiki借来的基本代码。