This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
priyanaka jaiswal wrote: Volatile Variable is stored in main memory so that all thread can access the updated volatile variable.what is this main memory exactly in JVM ?How it stores volatile variable?
The "main memory" that's referred to here is just the RAM in your computer. Each thread may have local copies of shared variables, such as in L1 or L2 cache, so a thread running on one CPU might be dealing with its local on-chip cache copy of a variable, while a thread running on a different CPU might have its own, separate copy of that variable in its on-chip cache.
By marking a variable volatile, we're saying that's not allowed to happen. All access to that variable must be against a single copy--no local caching.
volatile has to do with the visibility issue which means other threads might possibly see an old value of a variable which has been operated by another thread, if it's modified by volatile, there would be no visibility issue on this variable in multithreaded environment.