title: Are java primitive ints atomic by design or by accident?
body: <p>Are java primitive integers (int) atomic at all, for that matter? Some experimentation with two threads sharing an int seems to indicate that they <em>are</em>, but of course absence of evidence that they are <em>not</em> does not imply that they are.</p>
  
  <p>Specifically, the test I ran was this:</p>
  
  <pre><code>public class IntSafeChecker {
      static int thing;
      static boolean keepWatching = true;
  
      // Watcher just looks for monotonically increasing values   
      static class Watcher extends Thread {
          public void run() {
              boolean hasBefore = false;
              int thingBefore = 0;
  
              while( keepWatching ) {
                  // observe the shared int
                  int thingNow = thing;
                  // fake the 1st value to keep test happy
                  if( hasBefore == false ) {
                      thingBefore = thingNow;
                      hasBefore = true;
                  }
                  // check for decreases (due to partially written values)
                  if( thingNow &lt; thingBefore ) {
                      System.err.println("MAJOR TROUBLE!");
                  }
                  thingBefore = thingNow;
              }
          }
      }
  
      // Modifier just counts the shared int up to 1 billion
      static class Modifier extends Thread {
          public void run() {
              int what = 0;
              for(int i = 0; i &lt; 1000000000; ++i) {
                  what += 1;
                  thing = what;
              }
              // kill the watcher when done
              keepWatching = false;
          }
      }
  
      public static void main(String[] args) {
          Modifier m = new Modifier();
          Watcher w = new Watcher();
          m.start();
          w.start();
      }
  }
  </code></pre>
  
  <p>(and that was only tried with java jre 1.6.0_07 on a 32bit windows PC)</p>
  
  <p>Essentially, the Modifier writes a count sequence to the shared integer, while the Watcher checks that the observed values never decrease. On a machine where a 32 bit value had to be accessed as four separate bytes (or even two 16bit words), there would be a probability that Watcher would catch the shared integer in an inconsistent, half-updated state, and detect the value decreasing rather than increasing. This should work whether the (hypothetical) data bytes are collected/written LSB 1st or MSB 1st, but is only probablistic at best.</p>
  
  <p>It would seem very probable given today's wide data paths that a 32 bit value could be effectively atomic, even if the java spec doesn't require it.  In fact, with a 32 bit data bus it would seem that you might have to work harder to get atomic access to <em>bytes</em> than to 32 bit ints.</p>
  
  <p>Googling on "java primitive thread safety" turns up loads of stuff on thread-safe classes and objects, but looking for the info on the primitives seems to be looking for the proverbial needle in a haystack.</p>