Why does DCL double check lock need two layers of judgment?

We know that DCL double lock is written as:

synchronized (SingletonLazy5.class) {
    if(null == singletonLazy) {
        singletonLazy = new SingletonLazy5();
    }
}
Mar.02,2021

because once the singletonLazy is initialized correctly, each visit to null = = singletonLazy must get false so that it will not be reinitialized. But we don't need to judge null = = singletonLazy by locking every time. So the purpose of dual locking is to successfully initialize the singletonLazy without triggering the locking operation.

Menu