We know that DCL double lock is written as:
synchronized (SingletonLazy5.class) {
if(null == singletonLazy) {
singletonLazy = new SingletonLazy5();
}
}
We know that DCL double lock is written as:
synchronized (SingletonLazy5.class) {
if(null == singletonLazy) {
singletonLazy = new SingletonLazy5();
}
}
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.