This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What does the key word 'volatile' mean?  RSS feed

 
Nigel Chou
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to use it?

Thanks!
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look out it's going to blow up!

Seriously, that's how the Java memory manager has to treat that variable. It restricts how it may reorder operations that act upon it and enforces that whenever it is read or written, it must be done directly to memory instead of in a register.

From this article: "reads and writes of volatile fields would be made directly to main memory, instead of to registers or the local processor cache, and that actions on volatile variables on behalf of a thread are performed in the order that the thread requested."

Scanning Google presents a few more technical documents on the subject. They are all about Java's memory model, of which the volatile keyword is a part.
[ December 24, 2004: Message edited by: David Harkness ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!