Email address:
Password:
SCJP/OCPJP 1.6 certification
Practice Exams and Training
SCJP Online Training » Discussions

What is the use of volatile?

The given explanation only tells us that volatile is used in variable declaration. What is the use of volatile keyword?

The explanation does not include anything about how volatile works because you don't need to know that for the exam. For the exam, the only thing you need to know about volatile is that you can only use it with variables, excluding method-local variables.


Basically, if you have several threads accessing the same variable synchronously volatile helps you make sure that each thread reads the most up-to-date content from the memory for that variable. It helps to prevent strange bugs that could happen when you have a lot of threads and variables doing the inter-communication between them.


If interested, take a look at this article:

https://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html


ExamLab © 2008 - 2024