Why is double-checked locking broken in Java?

痴心易碎 提交于 2019-11-27 14:22:59
meriton

The problem is not atomicity, it's ordering. The JVM is allowed to reorder instructions in order to improve performance, as long as happens-before is not violated. Therefore, the runtime could theoretically schedule the instruction that updates helper before all instructions from the constructor of class Helper have executed.

The assignment of the reference is atomic, but the construction is not! So as stated in the explanation, supposing thread B wants to use the singleton before Thread A has fully constructed it, it cannot create a new instance because the reference is not null, so it just returns the partially constructed object.

If you do not ensure that publishing the shared reference happens before another thread loads that shared reference, then the write of the reference to the new object can be reordered with the writes to its fields. In that case, another thread could see an up-to-date value for the object reference but out of date values for some or all of the object's state - a partially constructed object. -- Brian Goetz: Java Concurrency in Practice

Since the initial check for null is not synchronized there is no publication and this reordering is possible.

Several assignments may be needed to construct the instance of Helper inside the constructor, and the semantics allows that they are reordered with respect to the assignment helper = new Helper().

So the field helper may be assigned a reference to an object where not all assignments have taken place, so that it is incompletely initialized.

Double checked locking in java has a variety of problems:

http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

Read this article: http://www.javaworld.com/jw-02-2001/jw-0209-double.html Even if you did not understand all details (like me) just believe that this nice trick does not work.

I'm sorry this might be a bit irrelevant to the question, I'm just curious. In this case wouldn't it better to acquire the lock before the assignment and/or returning the value? Like:

private Lock mLock = new ReentrantLock();
private Helper mHelper = null;

private Helper getHelper() {
    mLock.lock();
    try {
        if (mHelper == null) {
            mHelper = new Helper();
        }
        return mHelper;
    }
    finally {
        mLock.unlock();
    }
}

Or is there any advantage of using the double-checked locking?

/*Then the following should work.
  Remember: getHelper() is usually called many times, it is BAD 
  to call synchronized() every time for such a trivial thing!
*/
class Foo {

private Helper helper = null;
private Boolean isHelperInstantiated;
public Helper getHelper() {
    if (!isHelperInstantiated) {
        synchronized(this) {
            if (helper == null) {
                helper = new Helper();
                isHelperInstantiated = true;
            }
        }
    }
    return helper;
}

// other functions and members...
}    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!