--- old/test/java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java 2013-11-04 11:49:44.425183554 +0800 +++ new/test/java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java 2013-11-04 11:49:43.857183529 +0800 @@ -33,6 +33,7 @@ import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; +import java.util.concurrent.locks.ReentrantLock; public class ThrowingTasks { static final Random rnd = new Random(); @@ -156,6 +157,7 @@ } static class CheckingExecutor extends ThreadPoolExecutor { + private final ReentrantLock lock = new ReentrantLock(); CheckingExecutor() { super(10, 10, 1L, TimeUnit.HOURS, @@ -163,8 +165,17 @@ tf); } @Override protected void beforeExecute(Thread t, Runnable r) { - allStarted.countDown(); - if (allStarted.getCount() < getCorePoolSize()) + final boolean lessThanCorePoolSize; + // Add a lock to sync allStarted.countDown() and + // allStarted.getCount() < getCorePoolSize() + lock.lock(); + try { + allStarted.countDown(); + lessThanCorePoolSize = allStarted.getCount() < getCorePoolSize(); + } finally { + lock.unlock(); + } + if (lessThanCorePoolSize) try { allContinue.await(); } catch (InterruptedException x) { unexpected(x); } beforeExecuteCount.getAndIncrement();