+/EE360P

distributed systems

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

Joseph RyanFinish 1acbff598

master
1.1 KiB33 linesraw
1public class testCyclicBarrier implements Runnable {
2    final static int SIZE = 5;
3    final static int ROUND = 5;
4
5    final MonitorCyclicBarrier gate;
6
7    public testCyclicBarrier(MonitorCyclicBarrier gate) { this.gate = gate; }
8
9    public void run() {
10        int index = -1;
11
12        for (int round = 0; round < ROUND; ++round) {
13            System.out.println("Thread " + Thread.currentThread().getId() +
14                               " is WAITING round:" + round);
15            try {
16                index = gate.await();
17            } catch (InterruptedException e) { e.printStackTrace(); }
18            System.out.println("Thread " + Thread.currentThread().getId() +
19                               " is leaving round:" + round);
20        }
21    }
22
23    public static void main(String[] args) {
24        MonitorCyclicBarrier gate = new MonitorCyclicBarrier(SIZE);
25        Thread[] t = new Thread[SIZE];
26
27        for (int i = 0; i < SIZE; ++i) {
28            t[i] = new Thread(new testCyclicBarrier(gate));
29        }
30
31        for (int i = 0; i < SIZE; ++i) { t[i].start(); }
32    }
33}