/*
Filename: SummerThread.java
Author: Professor Needham
Course: SI411

A thread class that sums up the contents of a shared
array.

Uses ReadWriteController class to govern access to array.

Total number of summations to be conducted is passed as an argument to the
constructor.
*/

class SummerThread extends Thread {

 ReadWriteController controller;
 int[] intArray;
 int numRuns;

 SummerThread(ReadWriteController controllerIn, int[] intArrayIn, int numRunsIn){
   controller = controllerIn;
   intArray = intArrayIn;
   numRuns = numRunsIn;
 }

 public void run() {
   System.out.println(" ++++ Running SummerThread ++++");
   for (int j=1; j < numRuns; j++){
     controller.startRead();
     int total = 0;
     for (int i = 0 ; i < intArray.length; i ++){
         total += intArray[i];
     } // end for i
     controller.endRead();
     System.out.println("Sum of array is "+total);
   try {
      sleep((int)(Math.random()*2000));
    }
    catch (InterruptedException ie){}
   } // end for j
    System.out.println(" ---- SummerThread complete ---- ");
 } // end run
}// end SummerThread


