RSS

Volatile Vs Static in JAVA

12 Sep

Static Variable: If two Threads(suppose Thread 1 and Thread 2) are accessing the same object and updating a variable which is declared as static then it means Thread 1 and Thread 2 can make their own local copy of the same object(including static variables) in their respective cache, so updating by Thread 1 to the static variable in its local cache wont reflect in the static variable for Thread 2 cache . Static variables are used in the Object Context where updating by one object would reflect in all the other objects of the same class but not in the Thread context where updating of one thread to the static variable will reflect the changes immediately to all the threads (in their local cache).

Volatile variable: If two Threads(suppose Thread 1 and Thread 2) are accessing the same object and updating a variable which is declared as volatile then it means Thread 1 and Thread 2 can make their own local cache of the Object except the variable which is declared as a volatile . So the volatile variable will have only one main copy which will be updated by different threads and updating by one thread to the volatile variable will immediately reflect to the other Thread. So the volatile variable is used in the Thread context.

for further understanding refer the following image.

volatileVsStaticinJava

Advertisements
 
10 Comments

Posted by on September 12, 2013 in java

 

Tags: ,

10 responses to “Volatile Vs Static in JAVA

  1. Monir

    September 13, 2013 at 5:56 pm

    awesome

     
  2. Allani Abderraouf

    September 16, 2013 at 1:21 pm

    thanks!

     
  3. Kalpa

    November 27, 2013 at 3:41 pm

    Please help me to understand. When an variable defined as static; it says There is only one
    occurrence of a class variable per JVM per class loader.
    But your explanation talking about two local copies of static variable. Isn’t it contradict?

     
    • malalanayake

      November 27, 2013 at 3:59 pm

      Each thread can be seen as having it’s own copy of the field in it’s own local memory space/cache unless the field has been marked with volatile. If you need to share some data with in multiple threads please make it as volatile.

       
  4. syed

    March 10, 2014 at 4:52 pm

    Great Explanation!!

     
  5. sekhar

    July 4, 2014 at 7:17 am

    can you please clarify me what is the problem with code:

    Q: Even though i am not declaring the blinker as volatile, but thread t1 able to see the updated value(true) set by the main thread….

    code:
    package com.learning;

    public class HowToStopRunningThread implements Runnable{

    /**
    * @param args
    */
    public static boolean blinker;

    public static void main(String[] args) {
    Thread t = new Thread(new HowToStopRunningThread());
    t.start();
    HowToStopRunningThread obj = new HowToStopRunningThread();
    obj.stop();
    }

    public void stop(){
    try{
    Thread.sleep(100);
    System.out.println(“Setting the Blinker value”);
    blinker = true;
    }catch(InterruptedException ie){
    ie.getMessage();
    }
    }

    @Override
    public void run() {
    while(!blinker){
    try{
    System.out.println(“blinker:”+blinker);
    Thread.sleep(1000);
    }catch(InterruptedException ie){
    ie.getMessage();
    }
    }
    }
    }

    Output:
    blinker:false
    Setting the Blinker value
    ———————————
    and then thread came out of the while loop

     
    • malalanayake

      July 4, 2014 at 3:42 pm

      The problem is threads can see the changes of static field but it is not referring the same static field in memory. So we cannot guarantee the changes. But if you set variable as volatile that means every thread refers the same memory location and it’s guarantee that the visibility of every changes with in different threads. In your example it may update the value quickly because of the one thread. You better check with more thread then you can see some thread get delay to appear the changes.

       
  6. amila

    April 3, 2015 at 4:17 am

    In thread context it manage their own local copy.how that static variable reflect their values may be some time delay ? I know we cant garanty.but somehow it will reflect their values
    Grate post. Keep going with valuable ideas

     
  7. Madhava Nadendla

    February 17, 2016 at 8:56 am

    superb explanation…

     
  8. javaskyway

    October 6, 2016 at 10:31 am

    I don’t know either you have copied this content from http://stackoverflow.com/questions/2423622/volatile-vs-static-in-java
    or stackoverflow members copied your content.

    can you please proof this by taking any simple example in Java

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: