title: Share local variable value between barrier threads in java
body: <p>I've been working on implementing a custom <strong>Cyclic Barrier</strong> which adds values passed into the await method and returns the sum to all threads when after notify is called.</p>
  <p>The code:</p>
  <pre><code>public class Barrier {
  
      private final int parties;
      private int partiesArrived = 0;
  
      private volatile int sum = 0;
      private volatile int oldSum = 0;
  
      public Barrier(int parties) {
          if (parties &lt; 1) throw new IllegalArgumentException(&quot;Number of parties has to be 1 or higher.&quot;);
          this.parties = parties;
      }
  
      public int getParties() { return parties; }
  
      public synchronized int waitBarrier(int value) throws InterruptedException {
          partiesArrived += 1;
          sum += value;
          if (partiesArrived != parties) {
              wait();
          }
          else {
              oldSum = sum;
              sum = 0;
              partiesArrived = 0;
              notifyAll();
          }
          return oldSum;
      }
  
      public int getNumberWaiting() { return partiesArrived; }
  }
  
  </code></pre>
  <p>This works, but I hear that there is a way to change the values <code>sum</code> and <code>oldSum</code> (or at least <code>oldSum</code>) into local variables of the <code>waitBarrier</code> method. However, after racking my head over it, I don't see a way.</p>
  <p>Is it possible and , if yes, how?</p>