File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes A volatile example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A volatile example" Watch "A volatile example" New topic
Author

A volatile example

Owee Nicolas
Ranch Hand

Joined: Jun 16, 2002
Posts: 49
I'm studying the modifiers for class members. I'm having hard time understanding volatile Variables.
I read that it is use to inform the compiler that it should not attempt to perform optimizations on the variable.
Can anyone give me programming example where this is actually applied? Thanks!


Owee<br />SCJP 1.4
Snigdha Solanki
Ranch Hand

Joined: Sep 07, 2000
Posts: 128
You can refer to chapter " Use synchronized or volatile when accessing shared variables" from the book Practical Java by Peter Haggar. This chapter is available online at
http://www106.ibm.com/developerworks/java/library/praxis/pr50.html


Snigdha<br />Sun Certified Programmer for the Java™ 2 Platform
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
IMHO,
When your appz read a variable, he must first access that variable. The JVM make some optimization, it memorize the last value of your variable, so you don't lose time accessing your variable. The problem is if your variable is shared and it's value changed by a thread, and another thread want to read the value of your variable, he (the second thread) won't read the "real-time" value of your variable, but the copy in the JVM (result of the optimization).
So if you have a variable that value is often changed by another thread, you may use the Keyword 'Volatile' to tell the JVM "Don't use optimization on this variable"
I hope that I was clear! (I know that my english is sh....)


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Hi Snigdha,
I can't reach your link, could you plz check the url
Thx a lot
Sorry I found it, here is the good url
[ June 20, 2002: Message edited by: Younes Essouabni ]
Owee Nicolas
Ranch Hand

Joined: Jun 16, 2002
Posts: 49
I got that one! Thanks for all the help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A volatile example