aspose file tools*
The moose likes Threads and Synchronization and the fly likes volatile keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "volatile keyword " Watch "volatile keyword " New topic
Author

volatile keyword

Dheepan Thangavelu
Greenhorn

Joined: Oct 13, 2005
Posts: 4
what is the use of the keyword volatile?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

Here is a link to get you started:
http://www.javaperformancetuning.com/news/qotm030.shtml


Ed's latest article: A Java Parallel Calamity http://coopsoft.com/ar/Calamity2Article.html
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: volatile keyword