title: Synchronization of shared memory (int) with java threads
body: <p>In our app (written in Java for Android), we have a <code>Thread</code> derived class that contains an int field:</p>
  
  <pre><code>public class MyThread extends Thread {
  
      public int myValue = 0;
  
      public void doWork() {
  
          while (true) {
              System.out.println(myValue);
          }
      }
  }
  
  // On some other thread
  myThread.myValue = 42;
  </code></pre>
  
  <p>The <code>MyThread</code> class only <strong>reads</strong> from the int field, while i'd like another thread to <strong>write</strong> to it (as in the example above).</p>
  
  <p>As i know, primitive types such as int in Java are read/written atomically.</p>
  
  <p>Should i protect the access to this int field (using the <code>synchronized</code> keyword) ?</p>
  
  <p>I've read <a href="http://jeremymanson.blogspot.co.il/2008/11/what-volatile-means-in-java.html" rel="nofollow">here</a> that according to the Java memory model, updates to memory shared by a few threads may not even be seen if not explicitly communicated</p>
  
  <blockquote>
    <p>Quote: Without explicit communication, you can't guarantee which writes get
    seen by other threads</p>
  </blockquote>
  
  <p>Should i protect the access to this field or can another thread update it (atomically) without any needed modification?</p>