我需要一些線程在某檢查站同步,只有經過所有線程都達到了這一點,他們應該繼續。有沒有簡單的構造?爪哇 - 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