Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private int buffer[]; // Now an array of ints
- private int size; // Holds the length of the buffer
- public static int numItems; // Holds the number of items in the buffer
- private int getIndex;
- private int putIndex;
- public TentBufferMonitor(int bufSize) {
- // Initialise the buffer to a specified size
- size = bufSize;
- buffer = new int[size];
- // Initialise the indexes for getting and putting items (ints)
- getIndex = 0;
- putIndex = 0;
- }//TentBufferMonitor
- public synchronized int get() throws InterruptedException {
- int value;
- while (numItems == 0) {
- wait(); // if necessary wait for buffer to have items
- }//while
- value = buffer[getIndex];
- getIndex = (getIndex + 1) % size;
- numItems--;
- notifyAll(); // notify any waiting producers that buffer has space
- return value;
- } // get
- public synchronized void put(int value) throws InterruptedException {
- while (numItems == size) {
- wait();// wait for the buffer to have space
- System.out.println("Tent is currently at full capacity - Please wait!");
- }
- buffer[putIndex] = value; // place value in buffer
- putIndex = (putIndex + 1) % size;
- numItems++;
- notifyAll(); // notify the waiting consumer(s)
- Thread.sleep(3);
- } // put
- public static int numInTent(){
- return numItems;
- } //numInTent
- ** Exit 2 : 1 have just left the tent **
- ** Exit 1 : 1 have just left the tent **
- Entrance 2: 1 person entered the tent. Waiting time for entering 8
- Number of fans currently in tent: 0
- Entrance 1: 1 person entered the tent. Waiting time for entering 4
- Number of fans currently in tent: 0
- Entrance 2: 2 people have entered the tent. Waiting time for entering 4
- Number of fans currently in tent: 1
- Entrance 1: 2 people have entered the tent. Waiting time for entering 4
- Number of fans currently in tent: 2
- ** Exit 2 : 2 have just left the tent **
- ** Exit 1 : 2 have just left the tent **
Add Comment
Please, Sign In to add comment