title: Counting a single variable in multiple threads
body: <p>I've got the following runnable class.</p>
  
  <pre><code>public class OnesRun implements Runnable {
  
      public int ones = 0;
  
      private int passendNumber;
  
      public OnesRun(int passendNumber) {
          this.passendNumber = passendNumber;
      }
  
      public void run() {
          if (passendNumber == 1)
              ones++;
      }
  
  }
  </code></pre>
  
  <p>Every instance of this class should increase the value of <code>ones</code> if it encounters a one.</p>
  
  <p>After all threads have been executed I want to read the value of <code>ones</code> from outside the class.</p>
  
  <ol>
  <li>How can I increment <code>ones</code>
  thread-safe ? </li>
  <li>How can I access
  <code>ones</code> from outside this class?
  Through a static variable? Or can I put it into the application
  context?</li>
  </ol>
  
  <hr>
  
  <p><strong>Edit:</strong></p>
  
  <p>I hope the following pseudo code makes my intentions more clear.</p>
  
  <pre><code>OnesRun.ones = getCurrentValueOnes();
  
  while ( (number = readNumbersFromFile) != null) {
     threadPool.execute(new OnesRun(number));
  }
  
  print("Overall values of ones " + OnesRun.ones);
  </code></pre>