• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A volatile example

 
Owee Nicolas
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Snigdha Solanki
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got that one! Thanks for all the help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic