title: Java threads summing single value
body: <p>In Java, why in the code below <code>synchronized</code> can be commented? Because addition happens to be nearly atomic and thus the probability of error or failure is too small?</p>
  
  <pre><code>public class AddInParallel {
  
      public static void main(String[] args) {
  
          class sumValue {
              public int sum = 0;
              sumValue() {}
              public /*synchronized*/ void add(int i) {
                  sum += i;
              }
          }
  
          // final makes sumValue visible to threads
          final sumValue sum = new sumValue();
  
          class Adder implements Runnable {
  
              // used to distinguish threads
              private int id;
  
              Adder(int index) { id = index; }
  
              public void run() { sum.add(1); }
  
          }    
  
          // start threads
          Thread[] threads = new Thread[1000];
          for (int i = 0; i &lt; threads.length; i++) {
              threads[i] = new Thread(new Adder(i) {});
              threads[i].start();
          }
  
          // wait for threads above to finish
          for (int i = 0; i &lt; threads.length; i++) {
              try {
                  threads[i].join();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
  
          System.out.println(sum.sum);
      }
  }
  </code></pre>