Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Volatile keyword in atomicity  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to learn Volatile keyword in Multi Threading and I came across this statement:

Volatile is preferred in cases when one thread reads and writes a shared variable and other threads just read the same. Whereas if there are more than 2 threads performing read and write both on the shared variable then only volatile is not enough, you need to have synchronisation as well.  I am aware of the volatility concept and synchronisation, so can someone please provide an example relating to the above statement?
 
Sheriff
Posts: 23640
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

saurabhmehta mehta wrote:Volatile is preferred in cases when one thread reads and writes a shared variable and other threads just read the same.



Whereas if there are more than 2 threads performing read and write both on the shared variable then only volatile is not enough, you need to have synchronisation as well.



Seems to me it shouldn't be too hard to write your own examples. A variable, a couple of threads which read and/or write as required... why don't you give it a try? (However I don't know what you expect the examples to demonstrate.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!