title: Summing an array using multithreading
body: <p>I have an array with the size of n which is filled with the numbers 1..n.</p>
  
  <p>I need to sum this array using m threads by each time taking two elements, sum them up and inserting the sum back to the array.</p>
  
  <p>Here is what I tried to do.</p>
  
  <p>The synchronized part first</p>
  
  <pre><code>public class MultiThreadedSum {
  
  private ArrayBuffer ArrayBufferInst;
  private int Sum;
  private boolean Flag;
  
  public MultiThreadedSum(ArrayBuffer ArrayBufferInst)
  {
      this.ArrayBufferInst = ArrayBufferInst;
      Sum = 0;
      Flag = false;
  }
  
  public synchronized void Sum2Elements()
  {
  
      while(Flag)
      {
          try {wait();}
          catch (InterruptedException e){}
      }
  
      Flag = true;
  
      if (ArrayBufferInst.RetunrSize() &lt; 2)
      {
          return;
      }
  
      System.out.println("Removing 2 elements.");
  
      Sum = ArrayBufferInst.Sum2Elements();
  
      notifyAll();
  }
  
  public synchronized void InsertElement()
  {
  
      while(!Flag)
      {
          try {wait();}
          catch (InterruptedException e){}
      }
  
      Flag = false;
  
      System.out.println("Inserting the sum.");
  
      ArrayBufferInst.InsertElement(Sum);
  
      notifyAll();
  }
  
  public int RetunrSize()
  {
      return ArrayBufferInst.RetunrSize();
  }
  
  }
  </code></pre>
  
  <p>I've splitted the m threads to 2 groups, half of them will do the summarization and half will do the adding using wait and notify.</p>
  
  <pre><code>public class Sum2ElementsThread implements Runnable{
  
      private MultiThreadedSum MultiThreadedSumInst;    
  
      public Sum2ElementsThread( MultiThreadedSum MultiThreadedSumInst)
      {
          this.MultiThreadedSumInst = MultiThreadedSumInst;
      }
  
      @Override
      public void run() {
  
          while(MultiThreadedSumInst.RetunrSize() &gt; 1)
          {
              MultiThreadedSumInst.Sum2Elements();
          }
  
      }
  
  }
  
  public class InsertThread implements Runnable{
  
      private MultiThreadedSum MultiThreadedSumInst;    
  
      public InsertThread( MultiThreadedSum MultiThreadedSumInst)
      {
          this.MultiThreadedSumInst = MultiThreadedSumInst;
      }
  
      @Override
      public void run() {
  
          while(MultiThreadedSumInst.RetunrSize() &gt; 1)
          {
              MultiThreadedSumInst.InsertElement();
          }
  
      }
  
  }
  </code></pre>
  
  <p>Here is part of the main:</p>
  
  <pre><code>ArrayBufferInst = new ArrayBuffer(n);
  
  MultiThreadedSumInst = new MultiThreadedSum(ArrayBufferInst);
  
  ExecutorService Threads = Executors.newCachedThreadPool();
  
  for (i = 0; i &lt; m/2; i++)
  {
      Threads.execute( new Sum2ElementsThread(MultiThreadedSumInst) );
  }
  
  for (; i &lt; m; i++)
  {
      Threads.execute( new InsertThread(MultiThreadedSumInst) );
  }
  
  while (MultiThreadedSumInst.RetunrSize() &gt; 1){}
  
  Threads.shutdown();
  </code></pre>
  
  <p>And the buffer</p>
  
  <pre><code>public class ArrayBuffer {
  
      private ArrayList&lt;Integer&gt; ArrayBufferInst;
  
      public ArrayBuffer(int SizeOfBuffer)
      {
          int i;
  
          ArrayBufferInst = new ArrayList&lt;&gt;(SizeOfBuffer);
  
          for (i = 0; i &lt; SizeOfBuffer; i++)
          {
              ArrayBufferInst.add(i, i+1);
          }
      }
  
      public int Sum2Elements()
      {
          if (ArrayBufferInst.size() &lt; 2)
          {
              return -1;
          }
  
          return ArrayBufferInst.remove(0) + ArrayBufferInst.remove(1);
      }
  
      public void InsertElement(int Elem)
      {
          ArrayBufferInst.add(Elem);
      }
  
      public int RetunrSize()
      {
          return ArrayBufferInst.size();
      }
  }
  </code></pre>
  
  <p>I'm getting a lot of java.lang.IndexOutOfBoundsException and RangeCheck errors, it may be something to do with the way I implemented Sum2Elements but I'm not sure.</p>
  
  <p>Any help guys?</p>
  
  <p>Thanks.</p>