Your variables are confined to a single thread, so there is no other thread accessing them. Thus volatile
makes no difference.
If you declared them static
, they would be shared between different threads. However, even then you may not be able to observe the difference between your volatile and nonvolatile variable. Quote from Java Concurrency in Practice, chap. 3.1.4:
The visibility effects of volatile variables extend beyond the value of the volatile variable itself. When thread A writes to a volatile variable and subsequently thread B reads that same variable, the values of all variables that were visible to A prior to writing to the volatile variable become visible to B after reading the volatile variable. So from a memory visibility perspective, writing a volatile variable is like exiting a synchronized block and reading a volatile variable is like entering a synchronized block.
In your case, the code happens to modify the volatile variable first, so the updated value of the other variable may not be visible to the other thread. So far, so good.
However, since you are printing out the values of the variables from the same thread which modified them, you won't see any difference anyway.
Update2: Try this modified version (note: I have not tested it):
class ExampleThread extends Thread {
private static int testValue1;
private static volatile int testValue;
private int newValue;
public ExampleThread(String str, int newValue){
super(str);
this.newValue = newValue;
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(getName() + " testValue1 before update: " + testValue1);
System.out.println(getName() + " testValue before update: " + testValue);
testValue = i * newValue;
testValue1 = i * newValue;
System.out.println(getName() + " testValue1 after update: " + testValue1);
System.out.println(getName() + " testValue after update: " + testValue);
sleep(10);
}
}
}
public class VolatileExample {
public static void main(String args[]) {
new ExampleThread("Thread 1 ", 5).start();
new ExampleThread("Thread 2 ", 10).start();
}
}
Update: regarding the visibility of static fields - again from the same tome (chap 16.2.3):
[...] statically initialized objects require no explicit synchronization either during construction or when being referenced. However, this applies only to the as-constructed state - if the object is mutable, synchronization is still required by both readers and writers to make subsequent modifications visible and to avoid data corruption.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…