I have an array with the size of n which is filled with the numbers 1..n.

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.

Here is what I tried to do.

The synchronized part first

```
public class MultiThreadedSum {

    private ArrayBuffer ArrayBufferInst;
    private int Sum;
    private boolean Flag, StopFlag;

    public MultiThreadedSum(ArrayBuffer ArrayBufferInst) {
        this.ArrayBufferInst = ArrayBufferInst;
        Sum = 0;
        Flag = false;
        StopFlag = false;
    }

    public synchronized void Sum2Elements() {
        while(Flag){
            try {wait();}
            catch (InterruptedException e){}
        }
        Flag = true;
        if (StopFlag) {
            notifyAll();
            return;
        }
        System.out.println("Removing and adding 2 elements.");
        Sum = ArrayBufferInst.Sum2Elements();
        notifyAll();
    }

    public synchronized void InsertElement() {

        while(!Flag){
            try {wait();}
            catch (InterruptedException e){}
        }
        Flag = false;
        if (StopFlag) {
            notifyAll();
            return;
        }
        System.out.println("Inserting the sum.");
        ArrayBufferInst.InsertElement(Sum);
        if (ArrayBufferInst.RetunrSize() == 1) {
            StopFlag = true;
        }
        System.out.println(ArrayBufferInst);
        notifyAll();
    }

    public boolean ReturnStopFlag(){
        return StopFlag;
    }

    @Override
    public String toString(){
        return ArrayBufferInst.toString();
    }
}

```

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.

```
public class Sum2ElementsThread implements Runnable{
    private MultiThreadedSum MultiThreadedSumInst;    
    public Sum2ElementsThread( MultiThreadedSum MultiThreadedSumInst){
        this.MultiThreadedSumInst = MultiThreadedSumInst;
    }

    @Override
    public void run() {
        while(!MultiThreadedSumInst.ReturnStopFlag())
            MultiThreadedSumInst.Sum2Elements();
    }
}

public class InsertThread implements Runnable{
    private MultiThreadedSum MultiThreadedSumInst;    
    public InsertThread( MultiThreadedSum MultiThreadedSumInst) {
        this.MultiThreadedSumInst = MultiThreadedSumInst;
    }

    @Override
    public void run() {
        while(!MultiThreadedSumInst.ReturnStopFlag())  {
            MultiThreadedSumInst.InsertElement();
        }
    }
}

```

Here is part of the main:

```
ArrayBufferInst = new ArrayBuffer(n);

System.out.println("The Array");
System.out.println(ArrayBufferInst);

MultiThreadedSumInst = new MultiThreadedSum(ArrayBufferInst);

ExecutorService Threads = Executors.newCachedThreadPool();

for (i = 0; i < m/2; i++)
    Threads.execute( new Sum2ElementsThread(MultiThreadedSumInst) );
for (; i < m; i++)
    Threads.execute( new InsertThread(MultiThreadedSumInst) );
Threads.shutdown();
while(!MultiThreadedSumInst.ReturnStopFlag()){}
System.out.println("The sum of the array is " + MultiThreadedSumInst);

```

And the buffer

```
public class ArrayBuffer {
    private ArrayList<Integer> ArrayBufferInst;
    public ArrayBuffer(int SizeOfBuffer){
        int i;
        ArrayBufferInst = new ArrayList<>(SizeOfBuffer);
        for (i = 0; i < SizeOfBuffer; i++){
            ArrayBufferInst.add(i, i+1);
        }
    }

    public int Sum2Elements(){
        if (ArrayBufferInst.size() < 2){
            return -1;
        }
        return ArrayBufferInst.remove(0) + ArrayBufferInst.remove(0);
    }

    public void InsertElement(int Elem) {
        ArrayBufferInst.add(Elem);
    }
    public int RetunrSize(){
        return ArrayBufferInst.size();
    }
    @Override
    public String toString() {
        return ArrayBufferInst.toString();
    }
}

```

My question is about the end of the main, sometimes the program stop, sometime it doesn't, I know all the threads are exiting the run method because I checked that.

Sometimes I see the `The sum of the array is` message, sometimes I don't.