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.
No comments:
Post a Comment