File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ public class Barrier {
2+ public Barrier (int numThreads ) {
3+ // TODO
4+ }
5+
6+ public void await () {
7+ // TODO
8+ }
9+ }
Original file line number Diff line number Diff line change 1- import java .util .concurrent .CyclicBarrier ;
2-
31public class BarrierExample {
42
53 public static void main (String args []){
64 int numThreads = 10 ;
75
8- final CyclicBarrier b = new CyclicBarrier (numThreads );
6+ final Barrier b = new Barrier (numThreads );
97
108 for (int i =0 ; i <numThreads ; i ++) {
119 new Thread (new Worker (b )).start ();
Original file line number Diff line number Diff line change 1- import java .util .concurrent .CyclicBarrier ;
2- import java .util .concurrent .BrokenBarrierException ;
3-
41public class Worker implements Runnable {
52
6- private CyclicBarrier barrier ;
7- public Worker (CyclicBarrier barrier ) {
3+ private Barrier barrier ;
4+ public Worker (Barrier barrier ) {
85 this .barrier = barrier ;
96 }
107
@@ -13,17 +10,11 @@ public void log(String message) {
1310 }
1411
1512 public void run () {
16- try {
17- log ("running" );
18- log ("this should happen before reaching the barrier" );
13+ log ("running" );
14+ log ("this should happen before reaching the barrier" );
1915
20- barrier .await ();
21- log ("this should happen after the barrier" );
22- } catch (InterruptedException ex ) {
23- log ("damn I hate interrupts! Committing suicide: " + ex );
24- } catch (BrokenBarrierException ex ) {
25- log ("damn, the dam burst: " + ex );
26- }
16+ barrier .await ();
17+ log ("this should happen after the barrier" );
2718 }
2819
2920}
You can’t perform that action at this time.
0 commit comments