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.
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
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 ]
[...] 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
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
Joined: Dec 13, 2002
thanx Jim sorry Trupti, the example wasnt good I will try to find another one....