title: Assigning a object to a field defined outside a synchronized block - is it thread safe?
body: <p>Is there anything wrong with the thread safety of this java code? Threads 1-10 add numbers via sample.add(), and Threads 11-20 call removeAndDouble() and print the results to stdout. I recall from the back of my mind that someone said that assigning item in same way as I've got in removeAndDouble() using it outside of the synchronized block may not be thread safe. That the compiler may optimize the instructions away so they occur out of sequence. Is that the case here? Is my removeAndDouble() method unsafe?</p>
  
  <p>Is there anything else wrong from a concurrency perspective with this code? I am trying to get a better understanding of concurrency and the memory model with java (1.6 upwards).</p>
  
  <pre><code>import java.util.*;
  import java.util.concurrent.*;
  
  public class Sample {
  
      private final List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
  
      public void add(Integer o) {
          synchronized (list) {
              list.add(o);
              list.notify();
          }
      }
  
      public void waitUntilEmpty() {
          synchronized (list) {
              while (!list.isEmpty()) {
                  try { 
                      list.wait(10000);  
                   } catch (InterruptedException ex) { }
              }
          }
      }
  
      public void waitUntilNotEmpty() {
          synchronized (list) {
              while (list.isEmpty()) {
                  try { 
                      list.wait(10000);  
                   } catch (InterruptedException ex) { }
              }
          }
      }
  
      public Integer removeAndDouble() {
          // item declared outside synchronized block
          Integer item; 
          synchronized (list) { 
              waitUntilNotEmpty();
              item = list.remove(0);
          }
          // Would this ever be anything but that from list.remove(0)?
          return Integer.valueOf(item.intValue() * 2);
      }
  
      public static void main(String[] args) {
          final Sample sample = new Sample();
  
          for (int i = 0; i &lt; 10; i++) {
              Thread t = new Thread() {
                  public void run() {
                      while (true) {
                          System.out.println(getName()+" Found: " + sample.removeAndDouble());
                      }
                  }
              };
              t.setName("Consumer-"+i);
              t.setDaemon(true);
              t.start();
          }
  
          final ExecutorService producers = Executors.newFixedThreadPool(10);
          for (int i = 0; i &lt; 10; i++) {
              final int j = i * 10000;
              Thread t = new Thread() {
                  public void run() {
                      for (int c = 0; c &lt; 1000; c++) {
                          sample.add(j + c);
                      }
                  }
              };
              t.setName("Producer-"+i);
              t.setDaemon(false);
              producers.execute(t);
          }
  
          producers.shutdown();
          try {
              producers.awaitTermination(600, TimeUnit.SECONDS);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
  
          sample.waitUntilEmpty();        
          System.out.println("Done.");
      }
  }
  </code></pre>