title: Unexpected result in multithreaded program
body: <p>This simple program has a shared array and 2 threads:
  first thread - shows sum of values in the array.
  second thread - subtracts 200 from one cell of the array and adds 200 to another cell.</p>
  
  <p>I would expect to see the results: 1500 (sum of the array), 1300 (if the display occurs between the subtraction and the addition).</p>
  
  <p>But for some reason, sometimes 1100 and 1700 appear, which I can't explain...</p>
  
  <pre><code>public class MainClass {
  
      public static void main(String[] args) {
          Bank bank = new Bank();
          bank.CurrentSum.start();
          bank.TransferMoney.start();
      }
  }
  
  class Bank {
      private int[] Accounts = { 100, 200, 300, 400, 500 };
      private Random rnd = new Random();
  
      Thread CurrentSum = new Thread("Show sum") {
          public void run() {
              for (int i = 0; i &lt; 500; i++) {
                  System.out.println(Accounts[0] + Accounts[1] + Accounts[2]
                          + Accounts[3] + Accounts[4]);
              }
          }
      };
  
      Thread TransferMoney = new Thread("Tranfer"){
          public void run(){
              for(int i=0; i&lt;50000; i++)
              {
                  Accounts[rnd.nextInt(5)]-=200;
                  Accounts[rnd.nextInt(5)]+=200;
              }
          }
      };
  }
  </code></pre>