parallele-programmierung/u06-1/Barrier.java

23 lines
384 B
Java

package u06_1;
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;
}
}