Java concurrency: Countdown latch vs Cyclic barrier

前端 未结 14 2067
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  萌比男神i
    2020-11-29 15:25

    CountDownLatch is a count down of anything; CyclicBarrier is a count down for thread only

    assume there are 5 worker threads and one shipper thread, and when workers produce 100 items, shipper will ship them out.

    For CountDownLatch, the counter can be on workers or items

    For CyclicBarrier, the counter can only on workers

    If a worker falls infinite sleep, with CountDownLatch on items, Shipper can ship; However, with CyclicBarrier, Shipper can never be called

提交回复
热议问题