我需要一些线程在某检查站同步,只有经过所有线程都达到了这一点,他们应该继续。有没有简单的构造?爪哇 - Synchonize线程在某一点
Integer threadCount = 10;
for (int i = 0; i < threadCount; i++)
{
new Thread(() ->
{
try
{
doFirst();
synchronized (threadCount)
{
threadCount--;
while (threadCount > 0)
threadCount.wait();
threadCount.notifyAll();
}
doSecond();
}
catch (Exception e) { e.printStackTrace(); }
}).start();
}
// all threads are started, to wait until they've finished, call threadCount.wait();
退房的CyclicBarrier,CountDownLatch和死锁存在锁定notifyAll的()只能调用。 – user802421 2014-09-24 16:34:24
接下来,'try {...} finally {latch.countDown(); ''是你的朋友。 – 2014-09-24 16:57:11