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.

This entry was posted in J2EE. Bookmark the permalink.

15 Responses to Using the volatile keyword in Java

  1. Joe Cheng says:

    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

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

  3. paulus says:

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

  4. shailendra says:

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

  5. AJ says:

    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.

  6. amit says:

    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??

  7. andy says:

    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.

  8. senthilkumar says:

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

  9. vaishali says:

    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.

  10. Suresh says:

    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.

  11. H.kiran kumar says:

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

  12. shilpa nirkhe says:

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

  13. G.Sivakumar says:

    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.

  14. sebin says:

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

  15. Franz says:

    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?