class Barrier { private int nThreads; private int currentThreads; public Barrier(int nThreads) { this.nThreads = nThreads; } synchronized public int await() throws InterruptedException { int pos = ++currentThreads; if (currentThreads >= nThreads) { notifyAll(); } else { while (currentThreads < nThreads) { wait(); } } return pos; } }