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 thoughts on “Using the volatile keyword in Java”

  1. From
    “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.

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

  3. 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.

  4. 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.

  5. 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.

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

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

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

Comments are closed.