Understanding volatile keyword in java in details

What is Atomic ? The term atomic is related to the atom, once considered the smallest possible unit. When computer code is considered atomic it can not be interrupted during its execution, We can also define atomic code as code that can’t be found in an intermediate state.

Atomicity in Java - Java specifies that basic loading and storing of variable is the atomic. That means the value of the variable can’t be found in a interim state during the store not be changed in the middle of loading the variable to a register.

Java Memory Model – Unfortunately , java’s memory model is a bit more complex. Threads are allowed to hold the values of variable in local memory. In that case, when one thread changes the values of the variable, another thread may not see the changed value. This is particularly in loops where looping is controlled by a variable. The lopping may be have already loaded the value of the variable into the register and does not necessary notice when another thread changes the vales.

Solution 1 – One way to solve this problem is to provider setter/getter for the variable and make then synchronize using synchronized keyword in declaration. This will work because while getting lock all temporary values stored in register will get cleared. But this will be little complex solution when one easy and elegant solution is available.

volatile variable – The more elegant solution for this problem is volatile variable. If a variable is marked as volatile, every time variable is used it must be read from main memory. Similarly when every time variable is written it must be stored directly to memory.

When not to use volatile ? – This solution is feasible only when you don’t have any code in getter/setter methods which again uses/modify the variable value. Form example increment/decrement operation can not be synchronized using volatile variable. These operations are not atomic as it is as series of operation like retrieve-modify-store.

Reference: Book – Java Threads , Third Edition

Comments

  1. This is very true particularly when there is a threaded application running on a multi-core cpu. volatile ensures all the local copies of the variable is in synch.

    ReplyDelete
  2. Nice artielce , by the way just to add from my experience I think we should make a variable volatile when we want ensure that thread doesn' cache the value of variable ,e.g. in below code
    while(!stop) {
    doWork() ;
    }

    Thread can cache the value of "stop" instead of getting it from main memory every time and if in between any other thread changes the value , it would not be visible to this thread. making boolean variable "stop" as volatile ensures this will not happen.

    Thanks
    Javin
    How to avoid deadlock in Java code

    ReplyDelete

Post a Comment

Popular posts from this blog

State Design Pattern by Example

Eclipse command framework core expression: Property tester

Composite Design Pattern by example