• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with volatile modifier

 
Sandeep Ghosh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Ghanashyam Apte
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Junilu,Ghanashyam now my doubt is cleared
Sandeep
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic