Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Martin Jeffers - 59351795
- Edward Ryan - 10335899
- Andrew O'Hart - 10386963
- */
- class BoundedBuffer
- {
- private int nextIn = 0;
- private int nextOut = 0;
- private int bufferSize = 0;
- private int occupied = 0;
- private int delta = 0;
- private int Ins = 0;
- private int Outs = 0;
- private boolean dataAvailable = false;
- private boolean roomAvailable = true;
- private int [] buffer;
- private long [] time;
- private long totalTime;
- private double averageTime;
- private int bufferCount = 0;
- private long start;
- private long end;
- BoundedBuffer(int size0)
- {
- bufferSize = size0;
- buffer = new int[bufferSize];
- time = new long[bufferSize];
- }
- public void start() // code taken from main and converted into a setter method
- {
- start = System.currentTimeMillis();
- end = start + 60000;
- }
- public long endTime() // getter method
- {
- return end;
- }
- public synchronized void insertItem(int number) // added public
- {
- while(!roomAvailable)
- {
- try
- {
- wait();
- }
- catch (InterruptedException e)
- {
- }
- }
- buffer[nextIn] = number;
- time[nextIn] = System.currentTimeMillis();
- Ins++;
- bufferCount++;
- nextIn = (nextIn+1) % bufferSize;
- if(bufferSize == bufferCount)
- roomAvailable = false;
- else
- dataAvailable = true;
- notifyAll();
- }
- public synchronized void removeItem() // added public
- {
- while(!dataAvailable)
- {
- try
- {
- wait();
- }
- catch (InterruptedException e)
- {
- }
- }
- totalTime = totalTime + System.currentTimeMillis() - time[nextOut];
- nextOut = (nextOut+1) % bufferSize;
- Outs++;
- bufferCount--;
- if(bufferCount == 0)
- dataAvailable = false;
- else
- roomAvailable = true;
- notifyAll();
- }
- public synchronized void getBufferStats() // added public
- {
- delta = Ins-Outs-bufferCount;
- System.out.println("Delta = " + delta + " Occupied = " + bufferCount);
- }
- public void printAverageTime() // code taken from main
- {
- averageTime = (((double)totalTime) / (Outs));
- System.out.println("Average wait time : " + averageTime);
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- class Watcher extends Thread
- {
- private BoundedBuffer buff;
- Watcher(BoundedBuffer buff0)
- {
- buff = buff0;
- }
- public void run()
- {
- while(System.currentTimeMillis() < buff.endTime()) //changed from global variable 'end' in main - to getter method in buffer class
- {
- buff.getBufferStats(); // moved from after try block to before
- // sometimes at the end of the program watcher would sleep and re-awaken
- // to print this line between the "good-bye" lines
- try
- {
- Watcher.sleep(1000);
- }
- catch (InterruptedException e)
- {
- }
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- class Producer extends Thread
- {
- private int randomNum;
- private BoundedBuffer buff;
- Producer(BoundedBuffer buff0)
- {
- buff = buff0;
- }
- public void run()
- {
- while(System.currentTimeMillis() < buff.endTime()) //changed from global variable 'end' in main - to getter method in buffer class
- {
- randomNum = (int)((Math.random()) * 101);
- buff.insertItem(randomNum);
- randomNum = (int)((Math.random()) * 101);
- try
- {
- Producer.sleep(randomNum);
- }
- catch (InterruptedException e)
- {
- }
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- class Consumer extends Thread
- {
- private int randomNum;
- private BoundedBuffer buff;
- Consumer(BoundedBuffer buff0)
- {
- buff = buff0;
- }
- public void run()
- {
- while(System.currentTimeMillis() < buff.endTime()) //changed from global variable in main - to getter method in buffer class
- {
- buff.removeItem();
- randomNum = (int)((Math.random()) * 101);
- try
- {
- Consumer.sleep(randomNum);
- }
- catch (InterruptedException e)
- {
- }
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- public class Assignment1
- {
- public static void main(String [] args)
- {
- BoundedBuffer buff = new BoundedBuffer(Integer.parseInt(args[0]));
- Thread Producer0 = new Producer(buff);
- Thread Consumer0 = new Consumer(buff);
- Thread Watcher0 = new Watcher(buff);
- buff.start(); //sets buffers start and end time
- Producer0.start();
- Consumer0.start();
- Watcher0.start();
- try
- {
- Producer0.join();
- System.out.println("Good-Bye from Producer.");
- Consumer0.join();
- System.out.println("Good-Bye from Consumer.");
- Watcher0.join();
- System.out.println("Good-Bye from Watcher.");
- }
- catch (InterruptedException e)
- {
- }
- buff.printAverageTime(); // prints average time
- }
- }
Add Comment
Please, Sign In to add comment