The moose likes Threads and Synchronization and the fly likes volatile Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "volatile" Watch "volatile" New topic
Author

volatile

amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231
Consider the class:
public class SerialNumberGenerator {
private static volatile int serialNumber = 0;
public static int nextSerialNumber() {
return serialNumber++;
}
} ///:~

"The serialNumber field is volatile because it is possible for each thread to have a local stack and maintain copies of some variables there. If you define a variable as volatile, it tells the compiler not to do any optimizations that would remove reads and writes that keep the field in exact synchronization with the local data in the threads."
I did not understand this. What doed it imply
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
For optimization purposes, each thread may mainain its own copy of the shared variable. What that means is that if one thread modifies that variable, another thread may not "see" it immediately. To prevent this condition, that variable should be declared volatile.
Note that "volatile" doesn't replace "synchronized", -- you still need to synchronize access to mutable shared data.
[ April 25, 2003: Message edited by: Eugene Kononov ]
amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231
So, I guess it means that to prevent inconsistency variables have to be made volatile and methods or code blocks have to be declared as synchronized??
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Well, no. If you synchronize all of your code, you don't need volatile -- the use of the synchronized keyword enforces the necessary synchronization between local copies and main memory.
The "volatile" modifier is often used to avoid synchronization under certain circumstances. You really need to know what you're doing though; in particular, it doesn't work properly for long and double variables, and any operation modifying the variable needs to be atomic (or made atomic using synchronization). This means that the code you listed above is thread-unsafe! Just have a look at the disassembly of the nextSerialNumber() method:You see that the "serialNumber++" operation is not at all atomic, and if two threads call this method simultaneously they will end up with the same serial number. Whoever wrote this code was seriously confused about what "volatile" does. If in doubt, don't use it, use plain old synchronization.
- Peter
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
"Following up on your own posts is a sure sign of madness."
Originally posted by Peter den Haan:
The "volatile" modifier is often used to avoid synchronization under certain circumstances. [...] in particular [...] any operation modifying the variable needs to be atomic (or made atomic using synchronization).
Minor clarification -- even when you synchronize methods that update a volatile variable, you'll still have to make sure that the variable always contains valid values; it must not go through any invalid intermediate states during the course of an update.
- Peter
 
jQuery in Action, 2nd edition
 
subject: volatile
 
Threads others viewed
Create a class called Appliance
spot the difference: public static final
Experimenting with "volatile"
Variables
exam lab says "volatile can used to modify only instance var"
developer file tools