Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your variables are confined to a single thread, so there is no other thread accessing them. Thus <code>volatile</code> makes no difference.</p> <p>If you declared them <code>static</code>, 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 <a href="http://www.javaconcurrencyinpractice.com/" rel="nofollow noreferrer">Java Concurrency in Practice</a>, chap. 3.1.4:</p> <blockquote> <p>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.</p> </blockquote> <p>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.</p> <p>However, since you are printing out the values of the variables <em>from the same thread which modified them</em>, you won't see any difference anyway.</p> <p><strong>Update2:</strong> Try this modified version (note: I have not tested it):</p> <pre><code>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 &lt; 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(); } } </code></pre> <p><strong>Update:</strong> regarding the visibility of static fields - again from the same tome (chap 16.2.3):</p> <blockquote> <p>[...] statically initialized objects require no explicit synchronization either during construction or when being referenced. However, this applies only to the <em>as-constructed</em> 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.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload