Java concurrency: Countdown latch vs Cyclic barrier

前端 未结 14 2106
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 14:44

I was reading through the java.util.concurrent API, and found that

  • CountDownLatch: A synchronization aid that allows one or more threads to wait
14条回答
  •  心在旅途
    2020-11-29 15:00

    The main difference is documented right in the Javadocs for CountdownLatch. Namely:

    A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations of the countDown() method, after which all waiting threads are released and any subsequent invocations of await return immediately. This is a one-shot phenomenon -- the count cannot be reset. If you need a version that resets the count, consider using a CyclicBarrier.

    source 1.6 Javadoc

提交回复
热议问题