编程

Java 中的 CountDownLatch 指南

954 2024-06-26 10:25:00

1. 介绍

本文中,我们将提供 CountDownLatch 类的指南,并演示如何在几个实际示例中使用它。

本质上,通过使用 CountDownLatch,我们可以让线程阻塞,直到其他线程完成给定的任务。

2. 并发编程中使用

简单地说,CountDownLatch 有一个计数器(counter)字段,你可以根据需要递减。然后我们可以使用它来阻塞调用线程,直到它被减少到零。

如果我们正在进行一些并发处理,我们可以实例化 CountDownLatch,该计数器的值与我们想要处理的线程数相同。然后,我们可以在每个线程完成后调用  countdown(),保证调用 await() 的依赖线程将阻塞,直到工作线程完成。

3. 等待线程池完成

让我们通过创建一个 Worker 并使用 CountDownLatch 字段在它完成时发出信号来尝试这种模式:

public class Worker implements Runnable {
    private List<String> outputScraper;
    private CountDownLatch countDownLatch;

    public Worker(List<String> outputScraper, CountDownLatch countDownLatch) {
        this.outputScraper = outputScraper;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        doSomeWork();
        outputScraper.add("Counted down");
        countDownLatch.countDown();
    }
}

然后,让我们创建一个测试,用以证明我们可以通过 CountDownLatch 来等待 Worker 实例完成:

@Test
public void whenParallelProcessing_thenMainThreadWillBlockUntilCompletion()
  throws InterruptedException {

    List<String> outputScraper = Collections.synchronizedList(new ArrayList<>());
    CountDownLatch countDownLatch = new CountDownLatch(5);
    List<Thread> workers = Stream
      .generate(() -> new Thread(new Worker(outputScraper, countDownLatch)))
      .limit(5)
      .collect(toList());

      workers.forEach(Thread::start);
      countDownLatch.await(); 
      outputScraper.add("Latch released");

      assertThat(outputScraper)
        .containsExactly(
          "Counted down",
          "Counted down",
          "Counted down",
          "Counted down",
          "Counted down",
          "Latch released"
        );
    }

自然地,“Latch released” 将始终是最后一个输出,因为它依赖于于 CountDownLatch 的释放。

请注意,如果我们不调用 await(),我们将无法保证线程执行的顺序,因此测试将随机失败。

4. 线程池等待开始

如果我们以前面的例子为例,但这次启动了数千个线程,而不是五个,那么很可能许多早期的线程在我们对后期的线程调用 start() 之前就已经完成了处理。这可能会使尝试和再现并发问题变得困难,因为我们无法使所有线程并行运行。

为了解决这个问题,让我们让 CountdownLatch 的工作方式与前面的示例不同。我们可以阻塞每个子线程,直到所有其他子线程都开始,而不是在某些子线程完成之前阻塞父线程。

让我们修改 run() 方法,使其在处理之前阻塞:

public class WaitingWorker implements Runnable {

    private List<String> outputScraper;
    private CountDownLatch readyThreadCounter;
    private CountDownLatch callingThreadBlocker;
    private CountDownLatch completedThreadCounter;

    public WaitingWorker(
      List<String> outputScraper,
      CountDownLatch readyThreadCounter,
      CountDownLatch callingThreadBlocker,
      CountDownLatch completedThreadCounter) {

        this.outputScraper = outputScraper;
        this.readyThreadCounter = readyThreadCounter;
        this.callingThreadBlocker = callingThreadBlocker;
        this.completedThreadCounter = completedThreadCounter;
    }

    @Override
    public void run() {
        readyThreadCounter.countDown();
        try {
            callingThreadBlocker.await();
            doSomeWork();
            outputScraper.add("Counted down");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            completedThreadCounter.countDown();
        }
    }
}

现在,让我们修改测试,使其阻塞直到所有已开始的 Workers,然后取消阻塞 Worker,然后阻塞直到 Worker 完成:

@Test
public void whenDoingLotsOfThreadsInParallel_thenStartThemAtTheSameTime()
 throws InterruptedException {
 
    List<String> outputScraper = Collections.synchronizedList(new ArrayList<>());
    CountDownLatch readyThreadCounter = new CountDownLatch(5);
    CountDownLatch callingThreadBlocker = new CountDownLatch(1);
    CountDownLatch completedThreadCounter = new CountDownLatch(5);
    List<Thread> workers = Stream
      .generate(() -> new Thread(new WaitingWorker(
        outputScraper, readyThreadCounter, callingThreadBlocker, completedThreadCounter)))
      .limit(5)
      .collect(toList());

    workers.forEach(Thread::start);
    readyThreadCounter.await(); 
    outputScraper.add("Workers ready");
    callingThreadBlocker.countDown(); 
    completedThreadCounter.await(); 
    outputScraper.add("Workers complete");

    assertThat(outputScraper)
      .containsExactly(
        "Workers ready",
        "Counted down",
        "Counted down",
        "Counted down",
        "Counted down",
        "Counted down",
        "Workers complete"
      );
}

该模式对于尝试重现并发 bug 非常有用,因为它可以用来迫使数千个线程尝试并行执行一些逻辑。

5. 提早终止 CountdownLatch

有时,我们可能会遇到这样一种情况,即 Worker 在CountDownLatch 减下来之前错误地终止。这可能导致它永远不会达到零,await() 永远不会终止:

@Override
public void run() {
    if (true) {
        throw new RuntimeException("Oh dear, I'm a BrokenWorker");
    }
    countDownLatch.countDown();
    outputScraper.add("Counted down");
}

让我们修改我们之前的测试,使用 BrokenWorker,以显示 await() 将如何永远阻塞:

@Test
public void whenFailingToParallelProcess_thenMainThreadShouldGetNotGetStuck()
  throws InterruptedException {
 
    List<String> outputScraper = Collections.synchronizedList(new ArrayList<>());
    CountDownLatch countDownLatch = new CountDownLatch(5);
    List<Thread> workers = Stream
      .generate(() -> new Thread(new BrokenWorker(outputScraper, countDownLatch)))
      .limit(5)
      .collect(toList());

    workers.forEach(Thread::start);
    countDownLatch.await();
}

显然,这不是我们想要的行为——应用程序继续运行比无限阻塞要好得多。

为了解决这个问题,让我们在对 await() 的调用中添加一个 timeout 参数。

boolean completed = countDownLatch.await(3L, TimeUnit.SECONDS);
assertThat(completed).isFalse();

正如我们所见,测试最终会超时,await()  将返回 false

6. 结论

在这个快速指南中,我们已经演示了如何使用 CountDownLatch 来阻塞线程,直到其他线程完成一些处理。

我们还展示了如何通过确保线程并行运行来帮助调试并发问题。

这些例子的实现可以在 GitHub 上找到;这是一个基于 Maven 的项目,因此应该很容易按原样运行。

https://github.com/eugenp/tutorials/tree/master/core-java-modules/core-java-concurrency-advanced