Hi, here's my question
After the threads have terminated the program should print out the average length of time (in ms)
between the Producer inserting an integer and the Consumer removing that integer from the buffer.
This is the last part of my assignment and I just can't grasp it. HEEEEEEEEEEEEEEEEEEEEEEEELP!!!
here is my code
import java.util.*; import java.io.*; class BoundedBuffer{ int nextIn,nextOut,size,occupied,ins,outs; boolean dataAvailable = false; boolean roomAvailable = true; boolean timeElapsed = false; int time = 1; int [] buffer; private final int BUFFERTIME = 5; private long startTime,stopTime,totalTime; public BoundedBuffer(int size){ //Constructor for buffer this.size = size; buffer = new int[size]; } public synchronized void insertItem(int item) throws InterruptedException{ //Method to add an item to the buffer while(roomAvailable == false){ //Thread sleeps when there's no room in the buffer wait(); } buffer[nextIn] = item; nextIn = (nextIn + 1) % size; dataAvailable = true; ins++; startTime = System.currentTimeMillis(); // ***** GET TIME ***** occupied++; System.out.println("Item Added. " + occupied); if(occupied == size) roomAvailable = false; notifyAll(); } public synchronized void removeItem() throws InterruptedException{ //Method to remove an item from the buffer while(dataAvailable == false){ wait(); } int tmp = buffer[nextOut]; buffer[nextOut] = 0; nextOut = (nextOut + 1) % size; outs++; stopTime = System.currentTimeMillis(); // ****** GET TIME ****** totalTime +=(stopTime-startTime); occupied--; System.out.println("Item removed. " + occupied); roomAvailable = true; if(occupied == 0) dataAvailable = false; notifyAll(); } public synchronized void incTime() throws InterruptedException{ time++; if(time == BUFFERTIME) timeElapsed = true; } public long average(){ long avg = totalTime/outs; return avg; } } class Producer extends Thread{ private BoundedBuffer buffer; public Producer(BoundedBuffer b){ buffer = b; } public void run(){ while(buffer.timeElapsed == false) try { int i = ((int)((Math.random()*100) + 1)); buffer.insertItem(i); Thread.sleep((int)((Math.random()*100) + 1)); } catch (InterruptedException e) { System.out.println("Error in execution"); } } } class Consumer extends Thread{ private BoundedBuffer buffer; public Consumer(BoundedBuffer b){ buffer = b; } public void run(){ while(buffer.timeElapsed == false) try { buffer.removeItem(); Thread.sleep((int)(Math.random() * 100 + 1)); } catch (InterruptedException e) { System.out.println("Error in execution"); } } } class Watcher extends Thread{ private BoundedBuffer buffer; private final int BUFFERTIME = 5; public Watcher(BoundedBuffer b){ buffer = b; } public void run(){ while(buffer.time <= BUFFERTIME){ try{ Thread.sleep(1000); //Sleep for 1 second in ms System.out.println("Delta = " + ((buffer.ins-buffer.outs) - buffer.occupied) + " Occupied = " + buffer.occupied + " Time elapsed = " + buffer.time); buffer.incTime(); if(buffer.time == BUFFERTIME){ System.out.println("Average Time = " + buffer..average()); System.out.println("Threads terminated. Goodbye for now"); System.exit(0); } } catch (InterruptedException e){ System.out.println("Error in execution"); } } } } class Assignment_1{ public static void main(String [] args){ System.out.println("Please enter the size of the buffer: "); Scanner in = new Scanner(System.in); int size = in.nextInt(); in.close(); if(size <= 0){ System.out.println("Sorry, the size must be greater than 0."); System.exit(0); } BoundedBuffer buffer = new BoundedBuffer(size); Producer producer = new Producer(buffer); Consumer consumer = new Consumer(buffer); Watcher watcher = new Watcher(buffer); producer.start(); consumer.start(); watcher.start(); try{ producer.join(); consumer.join(); watcher.join(); } catch(InterruptedException e) { System.out.println("Error"); } } }
I can't seem to get my head around the average time for the question.
Any help is greatly appreciated