This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes volatile  confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "volatile  confusion" Watch "volatile  confusion" New topic
Author

volatile confusion

Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Friends,
I didnt understood what volatile is all about...I have searched this forum and gone through some of the threads, still not cleared.

I read the below from one thread-
To declare that a variable can be modified asynchronously by concurrent threads - use 'volatile'.

what does it mean>>>

Pls clarify.

Thanks in advance.


Javardhan:
Amol Fuke
Ranch Hand

Joined: Apr 08, 2005
Posts: 129
A variable that might be concurrently modified by multiple threads (without locks or a similar form of mutual exclusion) should be declared volatile. Variables declared to be volatile will not be optimized by the compiler because their value can change at any time.


"There are no mistakes, only lessons"
Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Amol,
Thanks for your early reply. I got some idea with your words, but still not clear; what I am thinking is- if we are not going to use 'synchronized' then mutiple threads can access our variable at a time.

If I want to restrict the variable in such a way that multiple threads should not access concurently then I will go with 'synchronized' keyword.
If I want to allow multiple threads concurently, simply I will forget about synchronization (forgive me if I am wrong).

Then whats the special use of 'volatile' here???

Thank you once again.
vipul patel
Ranch Hand

Joined: Oct 16, 2005
Posts: 146
Volatile:

A class-member variable might be shared between multiple threads. So each thread maintains its own working copy of this variable. now if the variable is not volatile, each thread will NOT reconcile its working copy with the master copy at object level. so just put volatile infront of all those variable which are shared among multiple threads. it will force all threads to reconcile their own copies of variable to master copy.

However volatile is not frequently used thing in java. better way to use synchronization.
Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Much Thanks Dilip. I got clear idea what volatile is for.

Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: volatile confusion
 
Similar Threads
What is best use of volatile keyword?
Reg : static and volatile
Volatile
volatile
volatile... is this not correct way?