lundi 17 août 2015

Why adding the second test in the Double-Checked-Locking?

Refering to this http://ift.tt/1H5Y3Fp, we have:

// "Double-Checked Locking" idiom
class Foo {
    private Helper helper;
    public Helper getHelper() {
        if (helper == null) {
            synchronized(this) {
                if (helper == null) {
                    helper = new Helper();
                }
            }
        }
        return helper;
    }    
    // other functions and members...
}

What is the purpose of the second test? Is it really possible that 2 threads could access the same critical section at the same time?

Aucun commentaire:

Enregistrer un commentaire