• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

volatile keyword

 
Dheepan Thangavelu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the use of the keyword volatile?
 
Henry Wong
author
Marshal
Pie
Posts: 21515
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dheepan Thangavelu:
what is the use of the keyword volatile?


It tells that compiler that this variable will be used by many threads at the same time. And not to create any cache copies, in the code that is compiled.

Henry
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a link to get you started:
http://www.javaperformancetuning.com/news/qotm030.shtml
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:


It tells that compiler that this variable will be used by many threads at the same time. And not to create any cache copies, in the code that is compiled.

Henry


You don't think thats a too overly compiler centric answer? When you say compiler are you speaking of hotspot and JIT or something? I think it may be better to tell the VM as a whole this information as opposed to the compiler, especially javac.

Note: it also prevents reading of long and double, float while their values are being changed (since they each take at least two writes to change their values). This is probably the only useful use if even this one is...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic