java - Volatile keyword does not work as expected with multiple instances of a class -


this question has answer here:

i have read in posts volatile (even if it's not static) variable shared among the threads. when 1 thread updates variable second thread gets updated value. when run below code on local machine, running java 7 . not giving expected results

code -

public class statcivolatile3 {      public static void main(string args[]) {         new examplethread2("thread 1 ").start();         new examplethread2("thread 2 ").start();     }  }  class examplethread2 extends thread {     private volatile int testvalue = 1;      public examplethread2(string str) {         super(str);     }      public void run() {         (int = 0; < 3; i++) {             try {                 system.out.println(getname() + " : " + i);                 if (getname().compareto("thread 1 ") == 0) {                     testvalue++;                     system.out.println("test value t1: " + testvalue);                 }                 if (getname().compareto("thread 2 ") == 0) {                     system.out.println("test value t2: " + testvalue);                 }                 thread.sleep(1000);             } catch (interruptedexception exception) {                 exception.printstacktrace();             }         }     } } 

and result -

thread 2  : 0 test value t2: 1 thread 1  : 0 test value t1: 2 thread 2  : 1 test value t2: 1 thread 1  : 1 test value t1: 3 thread 2  : 2 test value t2: 1 thread 1  : 2 test value t1: 4 

here can see thread t2 test value 1. can please me in understanding why happening?

thanks in advance!

my expected result - both threads seeing updated value

thread 1  : 0 test value t1: 2 thread 2  : 0 test value t2: 2 thread 1  : 1 test value t1: 3 thread 2  : 1 test value t2: 3 thread 1  : 2 test value t1: 4 thread 2  : 2 test value t2: 4 

testvalue instance variable, there's separate copy of variable each instance of class. you've created 2 instances:

   new examplethread("thread 1 ").start();    new examplethread("thread 2 ").start(); 

... , each has own copy of variable. thread 2 never updates value, why stays @ 1.

if want them share same variable, make static:

private static volatile int testvalue = 1; 

your statement:

i have read in posts volatile (even if it's not static) variable shared among the threads

... demonstrates misreading or misunderstanding of have read. changes volatile variable seen between threads if both access same variable, not mean instance variable shared between instances.

some comments point out 'testvalue++' not atomic operation. in general, it's true should avoid kind of operation on volatile variable you're sharing between threads. limited example, however, not concern, since 1 thread mutates value of variable.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -