[concurrency-interest] ThreadPoolExecutorTest occasionally fails with a broken barrier!?

Doug Lea dl at cs.oswego.edu
Wed Feb 14 09:03:55 EST 2007


Oliver Pfeiffer wrote:
> Hi,
> 
> I'm wondering why the given JUnit test (shown below) occasionally fails with
> a broken barrier on multi processor systems using Java 5. The repetitive
> test fails 27 times of 10.000 runs on my dual-core system. The test should
> check wheter the acquired maximum number of simultaneous pool threads are
> usable and that the pool doesn't fail even when the internal task queue is
> full (caller runs policy).
> 

>   public void testThreadPoolExecutor() throws InterruptedException {
>     final int threads = THREAD_POOL_EXECUTOR.getMaximumPoolSize();
>     final int loops = threads * 16;
>     final CountDownLatch latch = new CountDownLatch(loops);
>     final AtomicInteger counter = new AtomicInteger();
>     final CyclicBarrier barrier = new CyclicBarrier(threads + 1);
>     for (int i = 0; i < loops; i++) {
>       THREAD_POOL_EXECUTOR.submit(new Runnable() {
>         public void run() {
>           if (counter.incrementAndGet() <= barrier.getParties()) {

Presumably your intent is to have each thread hit one barrier.
But sometimes, one thread will hit two or more, and others will
hit zero. In those cases some will be waiting for a thread that
never arrives, timing out on barrier.

-Doug


More information about the Concurrency-interest mailing list