wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes volatile keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "volatile keyword" Watch "volatile keyword" New topic
Author

volatile keyword

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Can any one explain with some example the exact use of Volatile keyword?
I Have understood that it is used mainly when one is working with multiple threads.
But I need a solid example or an explanation..
Thanks in advance..
Trupti
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
I never come cross the key word volatile in a code. In fact, I never come across it even in a cert exam.
kishor rao
Ranch Hand

Joined: Dec 10, 2002
Posts: 35
Java does have a volatile keyword. I dunno what it does though....isn't the question more appropriate in the intermediate or advanced sections?
Daniela Ch
Ranch Hand

Joined: Dec 13, 2002
Posts: 96

The use of the volatile keyword allows method one and method two to be executed concurrently, but guarantees that accesses to the shared value for i and j occur exactly as many times, and in exactly the same order, as they appear to occur during execution of the program text be each thread.
Therefore, method two never observes a value for j greater than that for i, because each update to i must be reflected in the shared value for i before the update to j occurs.
It is possible, however, that any given invocation of method two might observe a value for j that is much greater than the value observed for i, because method one might be executed many times between the moment when method two fetches the value of i and the moment when method two fetches the value of j.
more here
hhmm... I am not sure about the second paragraphe...I would have tought it is the other way around, that method two never observes a value for *i* greater than that for *j*...but this example comes from ibm's web site and they are probably right...ok, can anyone confirm all this?
[ January 07, 2003: Message edited by: Daniela Ch ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Daniela Ch:

[...]
hhmm... I am not sure about the second paragraphe...I would have tought it is the other way around, that method two never observes a value for *i* greater than that for *j*...but this example comes from ibm's web site and they are probably right...ok, can anyone confirm all this?
[ January 07, 2003: Message edited by: Daniela Ch ]

Imagine thread 1 to enter one(), executing i++ - now i > j. It gets interrupted by thread 2, also calling one() and executing i++ - now i >> j. Thread 3 comes along and calls two(), printing the accurate values for i and j.
Without the volatile keyword, the threads were allowed to cache values for i and j, only guaranteed to be updating them from/to main memory at synchronization boundaries. Therefore, in the above example, the call to two() could just print "anything" for i (even partially updated values - only the high bits, for example)...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It seems no guarantees can be made in this code. Consider:

Case 1:
Thread A is executing one() - has executed i++ but not yet j++, so i == 1 and j == 0.
Thread B gets control and executes entire two() method while Thread A waits. Thread B will observe that i > j.

Case 2:
Thread A is executing two() and has evaluated i (which is 0) but not yet j.
Thread B gets control and executes entire one() method while Thread A waits. Now i == 1 and j == 1, but Thead A has already read i == 0 so now it only sees that j == 1. Thread A will observe that i < j.

Either of these is possible - or many more complex scenarios. It seems IBM's site is in error here.
[ January 08, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Daniela Ch
Ranch Hand

Joined: Dec 13, 2002
Posts: 96
thanx Jim
sorry Trupti, the example wasnt good
I will try to find another one....
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Jim is correct, of course...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: volatile keyword
 
Similar Threads
What is best use of volatile keyword?
volatile keyword
volatile keyword
Volatile Keyworld
volatile keyword