aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem with volatile  modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem with volatile  modifier" Watch "Problem with volatile  modifier" New topic
Author

Problem with volatile modifier

Sandeep Ghosh
Greenhorn

Joined: Mar 10, 2001
Posts: 22
Hi
I am having some problem in understanding the concept of volatile keyword in Java .In Khalid's book it has been mentioned that this variable's value can change asynchronously .The compiler should not attempt to optimize it. What it actually means.Thanks in advance
Sandeep

------------------
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

volatile is not mentioned in the exam objectives and none of the study guides that I have seen discuss it much. RHE has a whole four-sentence paragraph on it. Supposedly, volatile is not in common use and is of interest in multiprocessor environments. For purposes of the certification exam, I would say "Forget about it." Well, maybe not forget about it since it may come out in a question about reserved words, but other than that, "Don't worry about it."
Junilu

[This message has been edited by JUNILU LACAR (edited June 02, 2001).]


Junilu - [How to Ask Questions] [How to Answer Questions]
Ghanashyam Apte
Greenhorn

Joined: May 12, 2001
Posts: 4
Hello Sandeep,
See, everytime a thread manipulates a variable it copies its value in to its working memory.So if you use 'volatile' modifier-- it effectively tells compiler to look for master copy i.e main memory variable value and not working memory of thread.Otherwise thread will just work on ots copy and would not get updated value of variable.
I hope i cleared your doubt.If not plz refer to Java Lang.Specification on Sun's site
Ghanashyam
Originally posted by Sandeep Ghosh:
Hi
I am having some problem in understanding the concept of volatile keyword in Java .In Khalid's book it has been mentioned that this variable's value can change asynchronously .The compiler should not attempt to optimize it. What it actually means.Thanks in advance
Sandeep


Sandeep Ghosh
Greenhorn

Joined: Mar 10, 2001
Posts: 22
Thanks Junilu,Ghanashyam now my doubt is cleared
Sandeep
ratul banji
Ranch Hand

Joined: Mar 15, 2001
Posts: 108
Hi Sandeep,
Try to refer to JLS to understand properly the use of volatile keyword.
<code>
Rules for Volatile Variables
If a variable is declared volatile, then additional constraints apply to the actions of each thread.
Let T be a thread and let V and W be volatile variables.

A use action by T on V is permitted only if the previous action by T on V was load, and a load action by T on V is permitted only if the next action by T on V is use. The use action is said to be "associated" with the read action that corresponds to the load.
A store action by T on V is permitted only if the previous action by T on V was assign, and an assign action by T on V is permitted only if the next action by T on V is store. The assign action is said to be "associated" with the write action that corresponds to the store.
Let action A be a use or assign by thread T on variable V, let action F be the load or store associated with A, and let action P be the read or write of V that corresponds to F. Similarly, let action B be a use or assign by thread T on variable W, let action G be the load or store associated with B, and let action Q be the read or write of W that corresponds to G. If A precedes B, then P must precede Q. (Less formally: actions on the master copies of volatile variables on behalf of a thread are performed by the main memory in exactly the order that the thread requested.)
The load, store, read, and write actions on volatile variables are atomic, even if the type of the variable is double or long.
</code>
ur friend ..
Ratul
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with volatile modifier