Note that there are some explanatory texts on larger screens.

plurals
  1. POjava: `volatile` private fields with getters and setters
    text
    copied!<p>Should we declare the private fields as <code>volatile</code> if the instanced are used in multiple threads?</p> <p>In <a href="http://java.sun.com/docs/books/effective/">Effective Java</a>, there is an example where the code doesn't work without volatile:</p> <pre><code>import java.util.concurrent.TimeUnit; // Broken! - How long would you expect this program to run? public class StopThread { private static boolean stopRequested; // works, if volatile is here public static void main(String[] args) throws InterruptedException { Thread backgroundThread = new Thread(new Runnable() { public void run() { int i = 0; while (!stopRequested) i++; } }); backgroundThread.start(); TimeUnit.SECONDS.sleep(1); stopRequested = true; } } </code></pre> <p>The explanations says that</p> <pre><code>while(!stopRequested) i++; </code></pre> <p>is optimized to something like this:</p> <pre><code>if(!stopRequested) while(true) i++; </code></pre> <p>so further modifications of <code>stopRequested</code> aren't seen by the background thread, so it loops forever. (BTW, that code terminates without <code>volatile</code> on JRE7.)</p> <p>Now consider this class:</p> <pre><code>public class Bean { private boolean field = true; public boolean getField() { return field; } public void setField(boolean value) { field = value; } } </code></pre> <p>and a thread as follows:</p> <pre><code>public class Worker implements Runnable { private Bean b; public Worker(Bean b) { this.b = b; } @Override public void run() { while(b.getField()) { System.err.println("Waiting..."); try { Thread.sleep(1000); } catch(InterruptedException ie) { return; } } } } </code></pre> <p>The above code works as expected without using volatiles:</p> <pre><code>public class VolatileTest { public static void main(String [] args) throws Exception { Bean b = new Bean(); Thread t = new Thread(new Worker(b)); t.start(); Thread.sleep(3000); b.setField(false); // stops the child thread System.err.println("Waiting the child thread to quit"); t.join(); // if the code gets, here the child thread is stopped // and it really gets, with JRE7, 6 with -server, -client } } </code></pre> <p>I think because of the public setter, the compiler/JVM should never optimize the code which calls <code>getField()</code>, but <a href="http://www.ibm.com/developerworks/java/library/j-jtp06197/index.html">this article</a> says that there is some "Volatile Bean" pattern (Pattern #4), which should be applied to create mutable thread-safe classes. <strong>Update:</strong> maybe that article applies for IBM JVM only?</p> <p>The question is: which part of JLS explicitly or implicitly says that private primitive fields with public getters/setters must be declared as <code>volatile</code> (or they don't have to)?</p> <p>Sorry for a long question, I tried to explain the problem in details. Let me know if something is not clear. Thanks.</p>
 

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