Real Life Examples For CountDownLatch and CyclicBarrier

前端 未结 11 1334
余生分开走
余生分开走 2021-01-30 13:52

One example is given by one of our trainers when he was explaining difference between CountDownLatch and CyclicBarrier.

CountDownLatch: Suppose a stone can be lifted by

11条回答
  •  醉话见心
    2021-01-30 14:44

    Theoretical Difference:

    In CountDownLatch, main threads waits for other threads to complete their execution. In CyclicBarrier, worker threads wait for each other to complete their execution.

    You can not reuse same CountDownLatch instance once count reaches to zero and latch is open, on the other hand CyclicBarrier can be reused by resetting Barrier, Once barrier is broken.

    Real life example:--

    CountDownLatch: Consider a IT world scenario where manager divided modules between development teams (A and B) and he wants to assign it to QA team for testing only when both the teams completes their task.

    Here manager thread works as main thread and development team works as worker thread. Manager thread waits for development teams thread to complete their task.

    CyclicBarrier: Consider the same IT world scenario where manager divided modules between development teams (A and B). He goes on leave and asked both team to wait for each other to complete their respective task once both are done assign it to QA team for testing.

    Here manager thread works as main thread and development team works as worker thread. Development team threads wait for other development team threads after completing their task.

提交回复
热议问题