Easing the bit Java interviewer -Java Singleton pattern to achieve double-checked locking DCL Why must add volatile



For T t = new T();
fact, the following bytecode instruction is completed

_new 'org/openjdk/jol/T'
dup
INVOKESPECIAL org/openjdk/jol/T.<init> ()V
astore 1
return

When a new thread to half, m = 0, the reordering occurs
at this time to the threads 2! T has seen an example of a half-point of the initialization!

This probability is very small, but concurrently as Taobao, are likely to occur! it is necessary to add!

Published 423 original articles · won praise 1097 · Views 550,000 +

Guess you like

Origin blog.csdn.net/qq_33589510/article/details/105319129