Aaron Johnson Now with 50% less caffeine!

Posted
17 February 2003 @ 12am

Tagged
J2EE

Using the volatile keyword in Java

Using the volatile keyword in Java.

The volatile modifier is used when you are working with multiple threads.

The Java language allows threads that access shared variables to keep private working copies of the variables; this allows a more efficient implementation of multiple threads. These working copies need be reconciled with the master copies in the shared main memory only at prescribed synchronization points, namely when objects are locked or unlocked. As a rule, to ensure that shared variables are consistently and reliably updated, a thread should ensure that it has exclusive use of such variables by obtaining a lock that, conventionally, enforces mutual exclusion for those shared variables.


15 Comments

Posted by
Joe Cheng
17 February 2003 @ 9am

Careful, volatile is ignored or at least not implemented properly on many common JVM’s, including (last time I checked) Sun’s JVM 1.3.1 for Windows. There was an article on DDJ that could demonstrate this even on single-processor machines–the article is locked now but you can still download the code listing:

http://www.ddj.com/ftp/2002/2002_06/jqa0602.txt


Posted by
Monoranjan Gorai
15 December 2003 @ 4am

This site is extremely well to get answer for the common FAQs in java.


Posted by
paulus
27 February 2004 @ 10pm

I want to know about keyword at java. please give me about that information.
thanks


Posted by
shailendra
16 March 2004 @ 4am

What is the difference between transient and volatile keyword.when we should use transient and when volatile?


Posted by
AJ
16 March 2004 @ 7pm

From http://java.sun.com/docs/books/tutorial/java/javaOO/variables.html
“The volatile keyword is used to prevent the compiler from performing certain optimizations on a member.”

According to Joshua Bloch’s excellent book “Effective Java Programming Language Guide”, “… the volatile modifier guarantees that any thread that reads a field will see the most recently written value.”

“The transient marker is not fully specified by The Java Language Specification but is used in object serialization which is covered in Object Serialization to mark member variables that should not be serialized.”

I’d suggest reading chp. 10 of “Effective Java Programming Language Guide”, which speaks to serialization and the use of the transient keyword.


Posted by
amit
24 November 2004 @ 5am

if u declare a variable as volatile.then is it equivalent to say that when a thread tries to use it, then it’ll obtain a lock on it so that the other thread cant write into it until the lock is released by the first one??


Posted by
andy
16 January 2005 @ 1pm

if you declare a variable as volatile it will always read from and write to main memory - some threads are able to keep copies of shared variable values, but if these are not synced with the master copies, then data gets messed up. Volatile means the JVM always keeps checking main memory, rather than private thread records.


Posted by
senthilkumar
9 February 2005 @ 6am

Can any one tell the what is exact use of volatile
keyword in java


Posted by
vaishali
24 February 2005 @ 1am

The volatile modifier requests the Java VM to always access the shared copy of the variable so the its most current value is always read. If two or more threads access a member variable, AND one or more threads might change that variable’s value, AND ALL of the threads do not use synchronization (methods or blocks) to read and/or write the value, then that member variable must be declared volatile to ensure all threads see the changed value.


Posted by
Suresh
18 March 2005 @ 6am

if u use transistent keyword in ur code that varaiable can’t be serilaized ,when transferring data to remote m/c it will go as null.


Posted by
H.kiran kumar
13 May 2005 @ 2am

I need to know the exact meaning of volatile keyword in java


Posted by
shilpa nirkhe
22 June 2005 @ 7am

I need to know the exact meaning of volatile and transient keyword in java and where to use them


Posted by
G.Sivakumar
18 July 2005 @ 7am

Transient… Transient keyword tells they are not part of the persistent.Suppose variable is declared as transient they are not serialized (or)persisted . They are not part of the persistent state of an object.

Volatile… A volatile modifier is mainly used in mutiple threads. Java allows threads can keep private working copies of the shared variables(caches).These working copies need be updated with the master copies in the main memory.
But possible of data get messed up. To avoid this data corruption use volatile modifier or synchronized .volatile means everything done in the main memory only not in the private working copies (caches).( Volatile primitives cannot be cached ).
So volatile guarantes that any thread will read most recently written value.Because they all in the main memory not in the cache…Also volatile fields can be slower than non volatile.Because they are in main memory not in the cache.But volatile is useful to avoid concurrency problem.


Posted by
sebin
20 September 2005 @ 8am

What is the full meaning of System.out.println?


Posted by
Franz
1 November 2005 @ 10pm

When I have 2 tread accessing the same variable, one of them has only write access the other one only read access do have to use volatile?

I other words: Can I be sure that the reading thread get informed that the other thread changed the variable (needn’t be immediately) or can I get an endless loop when the reading thread waits for a change by the other one?