Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

volatile keyword

 
trupti nigam
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Daniela Ch
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx Jim
sorry Trupti, the example wasnt good
I will try to find another one....
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim is correct, of course...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic