File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Volatile Modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Volatile Modifiers" Watch "Volatile Modifiers" New topic

Volatile Modifiers

Mohammed Yousuff
Ranch Hand

Joined: Oct 17, 2007
Posts: 198
I read a document which say Volatile Modifiers, are not cached by the JVM. When ever a volatile variable value is required by a thread it will access directly from the variable itself (without using the JVM cache)..

Volatile: Volatile modifier tells the compiler that the variable modified by
volatile can be changed unexpectedly by other parts of the program. For
example a Variable might be read from Cache and not update the content if it
has been changed by another thread. Specifying a variable as volatile tells
the JVM that any threads using that variable are not allowed to cache that
value at all. Making the Variable Volatile will ensure that the compiler
will get the content of the variable every time it is used and not cache its
content. If not used Carefully this modifier might introduce bugs..

is my understanding is right ???... can you please share me in which real time scenarios we will use volatile modifiers....

My Thoughts :
Try not to become a man of success but rather to become a man of value.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19552

You only use it in a multi-threaded environment. A simple example:
Usually, run() is called in one thread (the one you start using the Runnable), and stop() is called from another thread (like the event thread in AWT / Swing or the main thread). If in line 1 the cached value of active is used, the loop may not stop when you set active to false in line 2. That's when you want to use volatile.

How To Ask Questions How To Answer Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 36566
Any variable can live in RAM, in Cache or in the registers on the chip. When a method finishes, the value may be stored in any of those plaecs in case it is needed again; if not it is moved back from register to cache to RAM. If two threads require access to a variable, they might pick it up from RAM, but there might be a more "recent" or "up-to-date" version in cache or a register. So(if I remember correctly) "volatile" means that methods must only use the "RAM" version of a variable, and when they have finished they must write their result back in RAM.

So the RAM version of a "volatile" variable is always the most "recent."
Henry Wong

Joined: Sep 28, 2004
Posts: 18135

I believe that it is unfortunate that the document used "cache" -- as people assumes that it is the hardware cache. I don't think it does.

The memory subsystems, along with the L1/L2 caches, should be able to keep everything in sync, and provide the latest versions. IMHO, when the document says cache, I think it means that it will not delay writes of register to memory, or assumes that the register has the latest version.

IOWs, don't use registers to cache the value -- I don't think it was referring to the hardware caches.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Pat Farrell

Joined: Aug 11, 2007
Posts: 4639

Originally posted by Henry Wong:
people assumes that it is the hardware cache.

Especially the idea of "the cache" is really dangerous. Modern machines have so many cache levels and in the server world, separate multi-core processors, that manual thinking about cache is bad.
I agree. Here's the link:
subject: Volatile Modifiers
Similar Threads
OCPJP sample question wrong on Oracles website
Volatile and Snoopy Caches
Volatile Modifiers
volatile and static keywords