Can Semaphore safely use double idiom checking?

Is the next Safe kernel in java?

public class TestDCL{
    private static final Semaphore lock = new Semaphore(1);
    private Object instance;

    public Object m(){
        if(instance == null){
            lock.acquire();
            if(instance == null){
                instance = new Object();
            }
            lock.release();
        }
        return instance; 
    }
}
+3
source share
3 answers

: . instance ? instance , volatile. , , , @Vijay Mathew, - . , singleton , . volatile , , .

Java Concurrency , 16.2.3 . 16.2.4 .

0

. new Object(); . instance null, . , if instance, . , Bill Pugh, .

+2

Tim. , , , ( -/JVM).

, , . ? , , , intialisation - , .

, . , ? , , syncronization per-sa. DCL .

+1

Source: https://habr.com/ru/post/1772597/


All Articles