What are volatile variables?
A
volatile
variable is modified asynchronously by concurrently running threads in a Java application.It is not allowed to have a local copy of a variable that is different from the value currently held in "main" memory. Effectively, a variable declared volatile
must have it's data synchronized across all threads, so that whenever you access or update the variable in any thread, all other threads immediately see the same value. Of course, it is likely that volatile
variables have a higher access and update overhead than "plain" variables, since the reason threads can have their own copy of data is for better efficiency.