• Post Reply Bookmark Topic Watch Topic
  • New Topic

volatile  RSS feed

 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!