Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

volatile keyword

 
Ranch Hand
Posts: 644
1
  • 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
 
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.
 
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?
 
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 ]
 
author
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)...
 
Wanderer
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim is correct, of course...
 
It's just a flesh wound! Or a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic