Forums Register Login

A volatile example

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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....)
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
I got that one! Thanks for all the help
This tiny ad is wafer thin:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1066 times.
Similar Threads
Volatile
volatile Vs synchronized
Questions about the fixed "volatile" keyword
is ++x threadsafe?
Wann volatile example
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:15:23.