, , volatile boolean, AtomicBoolean .
, :
- "" /
- while AtomicBoolean.compareAndSet
, , . , .
, , , .
To add my contribution, I noticed that you defined your atomic variable as volatile, and just in case, warn you that if you do not reassign this field and you can do it final, you better do it final. volatileapplies only to the actual value of the field, and if you do not change it (you do not use methods set/compareAndSet AtomicBoolean), and you make sure that other threads will see the initialized value ( finaldoes this), there is no point in using it.
source
share