Home arrow static arrow Java Programming [Archive] - Volatile Keyword
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Volatile Keyword
This topic has 5 replies on 1 page.

Posts:1
Registered: 6/18/03
Volatile Keyword  
Aug 3, 2004 12:54 PM



 
I am confused with the keyword "volatile". Few questions.

I have seen two different descriptions for this keyword.

1) Processors store data in their own registers for more efficient use.In Multiprocessor environments, the "volatile" keyword will ensure that a piece of shared data is always picked up from the (common) memory location and not reused from the (private copy) register for that processor.

If this is the case, then dont we require to use this keyword for all class level variables(static) ? Or does putting static keyword ensure this? (A static member is supposed to hold a common copy for all instances - how does it internally work in a multiprocessor environment?)
Is it possible that an instance may be partly serviced by one processor and partly by another in a multiprocessor env? In that case, dont we need to declare all instance variables as "volaile"?

2) "Volatile" keyword ensures that the data is "sequencially consistent" - meaning
if we have --
volatile int a = 5;
volatile boolean flag = true;

Then if flag is set to true, then a will already have been set to 5 always (this means this may not always be the case in the Runtime env, if the keyword is not volatile -- so in multithreading env..there can be problems if global variables are not "volatile"

Am I on the right track? The two descriptions of "volatile" seem very different. Are they both correct? I also read that many JVMS dont implement this as of now..so where do we stand in the use\relevance of this keyword?

Thanks,
Mathew Samuel.
 

Posts:3,055
Registered: 18/06/98
Re: Volatile Keyword  
Aug 3, 2004 1:08 PM (reply 1 of 5)



 
You need to search for "Java Memory Model" in java.sun.com and read the presentation in JavaOne 2004 TS-2331 - The New Java Technology Memory Model. ( http://java.sun.com/javaone, requires free registration and a PDF reader ).
 

Posts:3,055
Registered: 18/06/98
Re: Volatile Keyword  
Aug 3, 2004 1:10 PM (reply 2 of 5)



 
The presentation is

[url http://javaoneonline.mentorware.net/servlet/mware.servlets.StudentServlet?mwaction=generic&subsys_id=2000&file=free_session_register&class_code=TS-2331(USA,2004)&pdf=true] here [/url] .
 

Posts:13,769
Registered: 00-11-29
Re: Volatile Keyword  
Aug 3, 2004 1:14 PM (reply 3 of 5)



 
If this is the case, then dont we require to use this
keyword for all class level variables(static) ? Or
does putting static keyword ensure this? (A static
member is supposed to hold a common copy for all
instances - how does it internally work in a
multiprocessor environment?)

First, lets correct one of your statements. You say, "A static member is supposed to hold a common copy for all instances" It may seem subtle, but a static variable is associated with the class, not any instance. There doesn't need to be any instances of a class for a static to exist. The point is that there is not a 'copy' for each instance.

In a multi-threaded environment, each thread can cache it's own data. This is for performance. It takes time to keep the threads from interfering with each other.

Is it possible that an instance may be partly serviced
by one processor and partly by another in a
multiprocessor env? In that case, dont we need to
declare all instance variables as "volaile"?

If you synchronize access to variables, you don't need to declare things as volatile. volatile isn't used all that often. Mainly, it's for certain situations where full synchrionization is not required.

2) "Volatile" keyword ensures that the data is
"sequencially consistent" - meaning
if we have --
volatile int a = 5;
volatile boolean flag = true;

I can't really confirm or deny this.
 

Posts:1,183
Registered: 1/23/02
Re: Volatile Keyword  
Aug 3, 2004 1:16 PM (reply 4 of 5)



 
I am confused with the keyword "volatile".

Both (1) and (2) are correct. They are the two complementary guarentees that "volatile" gives you. JSR133 (implemented in 1.5) adds even more guarantees. As far as I know all JVMs worth mentioning implement these guarantees correctly.

Keyword "synchronized" has a lot to do with memory guarantees too. You can get by without "volatile" by using "synchronized" appropriately. This is why you don't see lots and lots of variables declared "volatile" and things still work.
 

Posts:3,258
Registered: 00-08-28
Re: Volatile Keyword  
Aug 3, 2004 2:33 PM (reply 5 of 5)



 

First, lets correct one of your statements. You say,
"A static member is supposed to hold a common copy for
all instances" It may seem subtle, but a static
variable is associated with the class, not any
instance. There doesn't need to be any instances of a
class for a static to exist. The point is that there
is not a 'copy' for each instance.

exactly in simple words, the volatile keyword designates a variable whose value my change between explicit reads and writes. In simple words variables declared volatile are stored in the Main memory rather than the working memory of the thread.

 
This topic has 5 replies on 1 page.