+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
df69975
master
1class BoundedBuffer { 2final int size =10 ; 3Object []buffer =new Object [size ]; 4int inBuf =0 ,outBuf =0 ; 5BinarySemaphore mutex =new BinarySemaphore (true ); 6CountingSemaphore isEmpty =new CountingSemaphore (0 ); 7CountingSemaphore isFull =new CountingSemaphore (size ); 8 9public void deposit (Object value ) { 10isFull .P ();// wait if buffer is full 11mutex .P ();// ensures mutual exclusion 12buffer [inBuf ] =value ;// update the buffer 13inBuf = (inBuf +1 ) %size ; 14mutex .V (); 15isEmpty .V ();// notify any waiting consumer 16 } 17public Object fetch () { 18Object value ; 19isEmpty .P ();// wait if buffer is empty 20mutex .P ();// ensures mutual exclusion 21value =buffer [outBuf ];//read from buffer 22outBuf = (outBuf +1 ) %size ; 23mutex .V (); 24isFull .V ();// notify any waiting producer 25return value ; 26 } 27} 28