Understanding the conditions behind using volatile variable

Rohit Bajaj
Rohit Bajaj used Ask the Experts™
on
Hi,
I came across the following in the book java concurrency in practice :
Screen-Shot-2019-03-13-at-2.32.25-PM.png
Frankly speaking i dont understand why all of these conditions are required.
Unable to find any reasons for all this.

Please help me understand it.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Volatile means that the var has to be read from main memory so it can *be seen for both reads and writes with equal integrity.

(There's a bit more subtlety to it than that, but a read-up in the literature will explain it in more detail.)

* Sorry - edited typo.
Distinguished Expert 2018
Commented:
Volatile is a weaker form of synchronisation (volatile variables only guarantee visibility, not atomicity). Variables can be cached in registers or CPU caches and thus can be hidden from other processors. A volatile variable on the other hand is shared by everyone since it is stored in main memory.

The way I understand it, the conditions you read mean that you should use volatile only when

1) atomicity is not a requirement and
2) the volatile variable does not participate in other state variables that calculate a result and
3) you do not need any locking

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial