17. java并发中CountDownLatch的使用
private static final class Sync extends AbstractQueuedSynchronizer {
...
}
private final Sync sync;
public void countDown() {
sync.releaseShared(1);
} public void await() throws InterruptedException {
sync.acquireSharedInterruptibly(1);
}
public boolean await(long timeout, TimeUnit unit)
throws InterruptedException {
return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
}
主线程等待子线程全都结束之后再开始运行
等待所有线程都准备好再一起执行
停止CountdownLatch的await
最后更新于
这有帮助吗?