如何等待线程池中的所有作业完成?

时间:2016-02-06 19:10:56

标签: java multithreading threadpool executorservice

我有一个Threadpool,请看下面的代码。我现在想要的是之前 asserEquals语句等待所有作业完成。怎么能实现这一目标? awaitTermination()仅在已经调用shutdown但这不是我想要的情况下才有效。

private volatile int i = 0;

@Test
public void threadPoolTest() throws InterruptedException {
    ExecutorService  threadPool = Executors.newFixedThreadPool(8);
    threadPool.submit(new Runnable() {
        public void run() {
            i++;
        }
    });
    threadPool.submit(new Runnable() {
        public void run() {
            i++;
        }
    });
    assertEquals(2,i);
    threadPool .shutdown();
}

2 个答案:

答案 0 :(得分:3)

尝试使用CountDownLatch:它是一个并发障碍,可以等待N个任务,如计数器:

@Test
public void threadPoolTest() throws InterruptedException {
    ExecutorService  threadPool = Executors.newFixedThreadPool(8);

    final CountDownLatch latch = new CountDownLatch(2);

    threadPool.submit(new Runnable() {
        public void run() {
            i++;
            latch.countDown();
        }
    });
    threadPool.submit(new Runnable() {
        public void run() {
            i++;
            latch.countDown();
        }
    });
    latch.await();
    assertEquals(2,i);
    threadPool .shutdown();
}

答案 1 :(得分:1)

// tell the threadpool it shouldn't accept new tasks:
threadPool.shutdown();

// and wait until the already submitted tasks finish:
try {
  threadPool.awaitTermination(...);
  assertEquals(2,i);
} catch(InterruptedException e) {
  assertTrue(false); // shouldn't get here only if timeouts
}

如果您不想事先关闭池,那么您可以使用CountDownLatch并计算直到线程完成。 它的问题在于,理论上,即使你开始计算"倒计时,其他一些线程也可以添加到池中,这就是为什么通常你关闭所以以后不能再添加其他任务。

相关问题