+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

vijaygarg1add stack and queuecccc41d

master
1006 B41 linesraw
1import java.util.concurrent.locks.*;
2
3class MBoundedBufferMonitor {
4   final int size = 10;
5   final ReentrantLock monitorLock = new ReentrantLock();
6   final Condition notFull  = monitorLock.newCondition(); 
7   final Condition notEmpty = monitorLock.newCondition(); 
8
9   final Object[] buffer = new Object[size];
10   int inBuf=0, outBuf=0, count=0;
11
12   public void put(Object x) throws InterruptedException {
13     monitorLock.lock();
14     try {
15       while (count == buffer.length)
16         notFull.await();
17       buffer[inBuf] = x;
18       inBuf = (inBuf + 1) % size;
19       count++;
20       notEmpty.signal();
21     } finally {
22       monitorLock.unlock();
23     }
24   }
25
26   public Object take() throws InterruptedException {
27     monitorLock.lock();
28     try {
29       while (count == 0)
30         notEmpty.await();
31       Object x = buffer[outBuf];
32       outBuf = (outBuf + 1) % size;
33       count--;
34       notFull.signal();
35       return x;
36     } finally {
37       monitorLock.unlock();
38     }
39   }
40 }
41