At work today, I came across the volatile
keyword in Java. Not being very familiar with it, I found this explanation.
Given the detail in which that arti
volatile
=> synchronized
[About]
volatile
says for a programmer that the value always will be up to date. The problem is that the value can be saved on different types of hardware memory. For example it can be CPU registers, CPU cache, RAM... СPU registers and CPU cache belong to CPU and can not share a data unlike of RAM which is on the rescue in multithreading envirompment
volatile
keyword says that a variable will be read and written from/to RAM memory directly. It has some computation footprint
Java 5
extended volatile
by supporting happens-before
[About]
A write to a volatile field happens-before every subsequent read of that field.
volatile
keyword does not cure a race condition
situation when several threads can write some values simultaneously. The answer is synchronized
keyword[About]
As a result it safety only when one thread writes and others just read the volatile
value