+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
77db134
master
1public class MonitorCyclicBarrier { 2int n ,in ,out ; 3Object canEnter ; 4Object canExit ; 5public MonitorCyclicBarrier (int parties ) { 6out =n =parties ; 7in =0 ; 8canEnter =new Object (); 9canExit =new Object (); 10 } 11 12public int await ()throws InterruptedException { 13final int index ; 14final long threadNum =Thread .currentThread ().getId (); 15synchronized (canEnter ) { 16while (out <n ) { 17// System.out.println("Thread " + threadNum + " waiting on " + 18// (n - out) + " threads to leave"); 19canEnter .wait (); 20 } 21 } 22 23synchronized (canExit ) { 24index =in ++; 25// System.out.println("Thread " + threadNum + " is index " + index) 26while (in <n ) {canExit .wait (); } 27if (out ==n ) { 28// System.out.println(index + " is the first one out"); 29out =0 ; 30 } 31canExit .notifyAll (); 32 } 33 34synchronized (canEnter ) { 35 ++out ; 36// System.out.println(index + " leaving"); 37if (out ==n ) { 38in =0 ; 39canEnter .notifyAll (); 40// System.out.println(index + " is the last one out"); 41 } 42 } 43return index ; 44 } 45}