Useness of Volatile variables in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Useness of Volatile variables in java

I know how volatile variables work , and the logic of this variables.although i does seen any Usefulness of volatiles in java. Pleas send an example (code) that show how volatiles can solves a problem that could not has been solved without volatiles. (Do not suggest web sites , i readed most of them)

15th Aug 2019, 1:14 PM
mohammad reza abbasniya
mohammad reza abbasniya - avatar
1 Answer
+ 1
volatile variable are used in threading concept in java .when a variable is declared volatile it provides that the threads don't create a local cache and it is fetched from the main memory every time the threads access it thus the thread accessing it cannot change the value provides the atomicity. main(String[] args) { volatile long var=1200000L; var--; //we cant do it throws exception
18th Aug 2019, 12:45 PM
Aadhithya
Aadhithya - avatar