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