01: package org.antlr.misc;
02:
03: /**A very simple barrier wait. Once a thread has requested a
04: * wait on the barrier with waitForRelease, it cannot fool the
05: * barrier into releasing by "hitting" the barrier multiple times--
06: * the thread is blocked on the wait().
07: */
08: public class Barrier {
09: protected int threshold;
10: protected int count = 0;
11:
12: public Barrier(int t) {
13: threshold = t;
14: }
15:
16: public synchronized void waitForRelease()
17: throws InterruptedException {
18: count++;
19: // The final thread to reach barrier resets barrier and
20: // releases all threads
21: if (count == threshold) {
22: // notify blocked threads that threshold has been reached
23: action(); // perform the requested operation
24: notifyAll();
25: } else
26: while (count < threshold) {
27: wait();
28: }
29: }
30:
31: /** What to do when everyone reaches barrier */
32: public void action() {
33: }
34: }
|