CyclicBarrier is a very interesting functionality for Concurrency in Java. Here is a quick example which shows how this is implemented
1. Imagine a scenario where two doctors are needed to see a patient. One doctor has to wait while the other gets available.
2. So a doctor waits at a "Barrier" for the second doctor to come and only then do both proceed with the patient
3. And this need to be Cyclic in nature for all the patients in the hospital, i.e the Barrier needs to be Cyclic in nature
4. This is a simple example which showcases the "Cyclic" nature of the CyclicBarrier. For a detailed demo, you can refer to this page
Output
Doctor Thread-1 : READY to see the Patient -BatMan
Doctor Thread-2 : READY to see the Patient -BatMan
Doctor Thread-2 : WORKING on Patient-BatMan since the team is here
Doctor Thread-1 : WORKING on Patient-BatMan since the team is here
Doctor Thread-2 : DONE with the Patient -BatMan
Doctor Thread-2 : READY to see the Patient -SuperMan
Doctor Thread-1 : DONE with the Patient -BatMan
Doctor Thread-1 : READY to see the Patient -SuperMan
Doctor Thread-1 : WORKING on Patient-SuperMan since the team is here
Doctor Thread-2 : WORKING on Patient-SuperMan since the team is here
Doctor Thread-2 : DONE with the Patient -SuperMan
Doctor Thread-2 : READY to see the Patient -IronMan
Doctor Thread-1 : DONE with the Patient -SuperMan
Doctor Thread-1 : READY to see the Patient -IronMan
Doctor Thread-1 : WORKING on Patient-IronMan since the team is here
Doctor Thread-2 : WORKING on Patient-IronMan since the team is here
Doctor Thread-1 : DONE with the Patient -IronMan
Doctor Thread-2 : DONE with the Patient -IronMan
Monday, March 14, 2016
Friday, March 11, 2016
CountDownLatch Example
Lets take a situation where one thread has to wait for a few other threads to complete before it can start functioning. A few such practical scenarios could be:
1. A file can only be processed by "Thread C" once it has been read by "Thread A" and cleaned by "Thread B"
2. Data from different sources need to be combined. Multiple threads are bringing in data. The final thread has to wait until all have brought in data; only then can it combine them
To achieve this one simple way could be:
1. To make the final thread "Thread.Join" the previous thread. So the final will work only when the previous has completed. But as you can see, this will not suit sooooo many scenarios one being if there are multiple previous threads.
2. Use a complicated code based on Concurrent.Collections framework. For example we can use SynchronousQueues for implementing a simple sequential Hand over of data for the other thread to work. But again what if there are multiple non sequential threads.
CountdownLatch simplifies this
1. We create a CountDownLatch with a variable telling how many threads will be be running before the final thread can start
2. CountDown() : All other threads call this once they have completed their tasks
3. await() : The final thread calls this and waits until all the other threads have called CountDown() on the latch. Once the Countdown is done by all threads, the final thread can start working
The example i am using below is a simple example based on the wait a father has to do while his baby is being delivered. :)
Doctor Delivers, Nurse Checks, Pediatrician checks and only then can the father hold the baby. So the Father has to wait while the baby is delivered and checked by the nurse and the Pediatrician. I am keeping this simple and not ensuring the sequential handover of the baby.
The code below is self explanatory
The output is:
Nurse : Watching Delivery and Nursing the Baby....
Doctor : Delivering Baby....
Peditritian : Looking after the Baby during Delivery. Slapping and S%^T
Nurse : Baby is Fine and Beautiful.
Peditritian : Done. Phew.
Doctor : Delivered the Baby.
Father : Finally I can hold the baby.
1. A file can only be processed by "Thread C" once it has been read by "Thread A" and cleaned by "Thread B"
2. Data from different sources need to be combined. Multiple threads are bringing in data. The final thread has to wait until all have brought in data; only then can it combine them
To achieve this one simple way could be:
1. To make the final thread "Thread.Join" the previous thread. So the final will work only when the previous has completed. But as you can see, this will not suit sooooo many scenarios one being if there are multiple previous threads.
2. Use a complicated code based on Concurrent.Collections framework. For example we can use SynchronousQueues for implementing a simple sequential Hand over of data for the other thread to work. But again what if there are multiple non sequential threads.
CountdownLatch simplifies this
1. We create a CountDownLatch with a variable telling how many threads will be be running before the final thread can start
2. CountDown() : All other threads call this once they have completed their tasks
3. await() : The final thread calls this and waits until all the other threads have called CountDown() on the latch. Once the Countdown is done by all threads, the final thread can start working
The example i am using below is a simple example based on the wait a father has to do while his baby is being delivered. :)
Doctor Delivers, Nurse Checks, Pediatrician checks and only then can the father hold the baby. So the Father has to wait while the baby is delivered and checked by the nurse and the Pediatrician. I am keeping this simple and not ensuring the sequential handover of the baby.
The code below is self explanatory
The output is:
Nurse : Watching Delivery and Nursing the Baby....
Doctor : Delivering Baby....
Peditritian : Looking after the Baby during Delivery. Slapping and S%^T
Nurse : Baby is Fine and Beautiful.
Peditritian : Done. Phew.
Doctor : Delivered the Baby.
Father : Finally I can hold the baby.
Subscribe to:
Posts (Atom)