/*         SI411 Operating Systems Fall AY07
                Java Programming Lab 3
     Due COB, Date: Friday, 16 November 2006

Lab Focus: A Java Monitor solution to the Reader/Writer problem.

Deliverables: Each team turn the following in by the above deadline:

a. A paper printout of your Averager.java and AReplace83.java
   classes as well as your averager.txt and replace83.txt files.

b. Send your instructor an e-mail with ALL the .java files needed to 
   run your lab as attachments as well as the .txt files your program
   created.

c. The handdrawn picture AND the filled in table required in step 2 of
 the below lab instructions.

Lab Instructions:

1. Compile Driver.java,ReadWriteController.java, SummerThread.java, and
WriteRandom83Thread.java (available from the SI411 web page). Run the program 
and observe its output.

2. Assume your program produces the following output
to the screen.  For the second "Wrote" line in the below, indicate
where EACH THREAD is in relation the wait set, entry set,
or owner of the ReadWriteController object lock while the line is being
executed.  In your handdrawn picture, show the above information as well
as the contents of the shared array. If it cannot be determined (because only
the JVM knows for sure!) whether a particular thread is sleeping or in
the entry or wait set, assume the thread is in the entry
or wait set (as appropriate).  Additionally, complete the following table
 to show where each thread is while the indicated line is executing.

      Thread    |  Entry   |  Lock   |  Wait   |  Executing outside
       Name     |   Set    |  Owner  |  Set    |  Monitor
   -----------------------------------------------------
                |          |         |         |
   writeRandom83|   y / n  |   y / n |  y / n  |  y / n
                |          |         |         |
   ----------------------------------------------------
                |          |         |         |
      sumArray  |   y / n  |   y / n |  y / n  |  y / n
                |          |         |         |
   ----------------------------------------------------
                |          |         |         |
       main     |   y / n  |   y / n |  y / n  |  y / n
                |          |         |         |

Assume the following output upon which to base the handdrawn picture
described above:

     Running Summer
     Running WriteRandom83
     Sum of array is 0
     Wrote 83 to cell 5
     Sum of array is 83
     Sum of array is 83
     Sum of array is 83
     Wrote 83 to cell 1    //This line is the one referenced above.
     Sum of array is 166
     Wrote 83 to cell 3
      -- SummerThread complete
     Wrote 83 to cell 3
     Wrote 83 to cell 3
      -- WriteRandom83Thread complete

3. Write a thread class called Averager that can be used to
compute the average of the current contents of a shared array.
Note that this class requires read access to the shared array
This thread should print the contents of the array to a file
called averager.txt, print a line to averager.txt saying that it
is about to determine the average of the values in the array, then
average the array, and print the average to the averager.txt file.
Wait for a random amount of time (up to 2 seconds), and then repeat
the above process (adding its output to the bottom of averager.txt).
The thread should do this 5 times before completing.

4. Write a thread class called Replace83 that searches through
the shared array and replaces all 83's found with your favorite
nonzero number.  Note that this class requires write access to the
shared array. This thread should print the current array contents
to the file replace83.txt, print a message to the replace83.txt files
saying that it is about to replace all the 83s with your desired number,
then replace all 83's currently in the array with a number of your
choice (not 83!), and then print the updated array contents to the
replace83.txt file.  Wait for a random amount of time (up to 2 seconds),
and then repeat the above steps for a total of 5 times before completing.

5. Modify Driver.java so that an instance of Averager and an instance
of Replace83 are created and started with the same priority in a manner
similar to the other threads that main contains.


*/


public class Driver {
 public final int NUM_RUNS = 6;

 // Note: both of the below shared by all the reader/writers
 private ReadWriteController readWriteController = new ReadWriteController();
 private int[] intArray;

 public Driver() {
   intArray = new int[10];
 }


 private void launch(){
    // create threads
    SummerThread sumArray =
           new SummerThread(readWriteController, intArray, NUM_RUNS);
    WriteRandom83Thread writeRandom83 =
           new WriteRandom83Thread(readWriteController, intArray, NUM_RUNS);
   // start threads running
   sumArray.start();
   writeRandom83.start();
 } // end launch

 public static void main(String args[]){
   Driver theDriver  = new Driver();
   theDriver.launch();
   System.out.println("Main done");
 } // main

} // Driver
