-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclicBarrierExample.java
More file actions
45 lines (36 loc) · 1.27 KB
/
CyclicBarrierExample.java
File metadata and controls
45 lines (36 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package basic;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierExample {
static class Task implements Runnable{
private CyclicBarrier barrier;
public Task(CyclicBarrier barrier){
this.barrier=barrier;
}
@Override
public void run() {
try{
System.out.println(Thread.currentThread().getName()+"is waiting on barrier");
barrier.await();
System.out.println(Thread.currentThread().getName()+"has crossed on barrier");
}
catch (InterruptedException ex){
}catch (BrokenBarrierException ex){
}
}
}
public static void main(String[] args) {
final CyclicBarrier cb = new CyclicBarrier(3, new Runnable() {
@Override
public void run() {
System.out.println("All parties have arrived at the barrier.");
}
});
Thread t1= new Thread(new Task(cb),"Thread1");
Thread t2= new Thread(new Task(cb),"Thread2");
Thread t3= new Thread(new Task(cb),"Thread3");
t1.start();
t2.start();
t3.start();
}
}