// Filename: Producer.java
// Author: Professor Needham
// Course: SI411
// A modification of the Chapter 4 example in Silberschatz's OS text.

import java.util.*;

public class Producer extends Thread {
   public Producer(BoundedBuffer b) {
      buffer = b;
   }

   public void run() {
   Date message;
   long currentTime;

      while (true) {
         int sleeptime = (int) (BoundedBuffer.NAP_TIME * Math.random() );

         System.out.println("Producer sleeping for " + sleeptime + " seconds");

         try { sleep(sleeptime*1000); }
         catch(InterruptedException e) {}

         // produce an item & enter it into the buffer
         message = new Date();
         currentTime = System.currentTimeMillis();
         System.out.println("Producer produced " + message+ " " +(currentTime-startTime));
         System.out.println();
         System.out.println(" From Producer: Please kill me via Control-C");
         System.out.println("   if I am busy-waiting while the buffer is full");
         System.out.println("   and the consumer thread has finished.");
         System.out.println();

         buffer.enter(message.toString() + "  "+ (currentTime-startTime));
      }
   }

   private  BoundedBuffer buffer;
   private final long startTime = System.currentTimeMillis();
}

