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

A thread class that writes 83s to random spots in a shared
array.

Uses ReadWriteController class to govern access
to array.

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


import java.util.*; // for Random

class WriteRandom83Thread extends Thread {

 ReadWriteController controller;
 int[] intArray;
 int numRuns;

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

 public void run() {
   int targetCell =0;
   System.out.println(" ++++ Running WriteRandom83 ++++ ");
    for (int j=1; j < numRuns; j++){
     targetCell = (int) (Math.random() * intArray.length);
     controller.startWrite();
     intArray[targetCell] = 83;
     controller.endWrite();

     System.out.println("Wrote 83 to cell " + targetCell);

       try {
      sleep((int)(Math.random()*2000));
    }
    catch (InterruptedException ie){}
   } // end while
   System.out.println(" ---- WriteRandom83Thread complete ----");
 } // end run

}// end WriteRandom83Thread

