Updated 4 September 2024
Count Down Latch in Java.
Before going on the topic we recall a topic electronic logic circuit to which we all are familiar.
A Electronic logic circuit performs a action when circuit is in high or low state or we can say 0 or 1.
Countdown latch functionality is something same like logic circuit.
A Countdown Latch is a versatile synchronisation tool and can be used for a number of purposes.Countdown latch can block a thread until other thread have complete assign task.
A useful property of a CountDownLatch is that it doesn’t require that threads calling count Down wait for the count to reach zero before proceeding, it simply prevents any thread from proceeding past an await until all threads could pass.
Example/Sample usage:
1. We can simply use countdown in api calling either from network or database, where we need multiple hit pa rally and a single result at the end.
Work flow
Step 1.
Initialise the countdown latch with count/number you need.
Ex- We have to make a network call 5 times.
COutnDownLatch myLatch= New CountDown(5);
Step 2.
Now call await method so that it will until the count will be zero.
myLatch.await();
step 3.
Call countDown after completion each thread or task Every time countDown call it will reduce the count value by one.
myLatch.countDown();
step 4.
It is the finally step. When all thread or task call countDown then the value of count will be zero. Now the main thread which is waiting for sub threads to complete the work will execute their work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
public class SampleCodeMainClass { public static void main(String[] args) { CountDownLatch sampleLatch = new CountDownLatch(5); // Random random = new Random(); SampleThread wt1 = new SampleThread(sampleLatch,3000); SampleThread wt2 = new SampleThread(sampleLatch, 5000); SampleThread wt3 = new SampleThread(sampleLatch, 7000); SampleThread wt4 = new SampleThread(sampleLatch, 9000); SampleThread wt5 = new SampleThread(sampleLatch, 11000); new Thread(wt1, "WT-11").start(); new Thread(wt2, "WT-22").start(); new Thread(wt3, "WT-33").start(); new Thread(wt4, "WT-44").start(); new Thread(wt5, "WT-55").start(); try { sampleLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Finishing after coutntDown Latch is down"); } } class SampleThread implements Runnable { private CountDownLatch sampleLatch; private int threadSleepTime; public SampleThread(CountDownLatch latch, int sleepTime) { this.sampleLatch = latch; this.threadSleepTime = sleepTime; } @Override public void run() { String threadName = Thread.currentThread().getName(); int delayTime = this.threadSleepTime; System.out.println(threadName + " Delay time" + delayTime + " in milliseconds."); try { Thread.sleep(delayTime); } catch (InterruptedException e) { e.printStackTrace(); } this.sampleLatch.countDown(); System.out.println(threadName + " finished"); } } |
Note: We can’t reset the count value after initialising CountDownLatch.
Conclusion :
We have discussed how to achieve single result by doing multiple threading or dividing task with the help CountDown Latch.
Thankyou for reading!!
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.