21 lines
368 B
Java
21 lines
368 B
Java
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;
|
|
}
|
|
} |