Real Life Examples For CountDownLatch and CyclicBarrier

前端 未结 11 1340
余生分开走
余生分开走 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:29

    In a hypothetical theater:

    • It is called Mutex if only one person is allowed to watch the play.
    • It is called Semaphore if N number of people are allowed to watch the play. If anybody leaves the Theater during the play then other person can be allowed to watch the play.
    • It is called CountDownLatch if no one is allowed to enter until every person vacates the theater. Here each person has free will to leave the theater.
    • It is called CyclicBarrier if the play will not start until every person enters the theater. Here a showman can not start the show until all the persons enter and grab the seat. Once the play is finished the same barrier will be applied for next show.

    Here, a person is a thread, a play is a resource.

提交回复
热议问题