Skip to content

Commit 15971e2

Browse files
committed
Replace Java's CyclicBarrier with a stub implementation
1 parent 9a82c3f commit 15971e2

3 files changed

Lines changed: 16 additions & 18 deletions

File tree

Barrier.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class Barrier {
2+
public Barrier(int numThreads) {
3+
// TODO
4+
}
5+
6+
public void await() {
7+
// TODO
8+
}
9+
}

BarrierExample.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import java.util.concurrent.CyclicBarrier;
2-
31
public 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();

Worker.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import java.util.concurrent.CyclicBarrier;
2-
import java.util.concurrent.BrokenBarrierException;
3-
41
public 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
}

0 commit comments

Comments
 (0)