CountDownLatch in JavaThe CountDownLatch class is another important class for concurrent execution. It is a synchronization aid that allows one or more than one thread to wait until a set of operations being performed in another thread is completed. It initializes with the count, which we pass to the constructor. The invocation of the countDown() method causes the await methods to block until the current count reaches zero, after which all waiting threads are released, and any subsequent invocations of await immediately return. CountDownLatch doesn't require that thread countdown() to wait for the count to reach zero before proceeding. It prevents any thread from proceeding past and await until all threads can pass. The CountDownLatch is used for multiple purposes based on the count value:
Constructor of CountDownLatchCountDownLatch provides a parameterized constructor. It accepts an integer value for the count. It constructs the CountDownLatch and initializes it with the given count value. Note: The constructor throws an IllegalArgumentException when the value of count is negative.Syntax Parameters count -> it is the number of times countdown() must be invoked before the thread can pass through await(). Methods of CountDownLatchThe CountDownLatch class has several methods which we can use for concurrency control. The CountDownLatch class also provides methods of java.lang.Object class because it is inherited by the CountDownLatch class. The methods of the java.lang.Object class are as follows:
If you don't have knowledge of the Object class methods, go through the following link: https://www.javatpoint.com/object-class The CountDownLatch provides the following methods: 1. await()The await() method cause the current thread to wait until one of the following is not done:
The await() method immediately returns when the value of the current count is set to zero. The await() method disabled the current thread for thread scheduling purposes when the current count is neither zero nor a negative number, and the current thread remains dormant until one of the following things happens:
Syntax: The await() method has the following syntax: It does not accept any value and does not return any value. Throws The await() method throws interruptedException when the current thread is interrupted while waiting. 2. await()It is another variation of the await() method that causes the current thread to wait until the following is not done:
The await() method immediately returns the value true when the value of the current count is set to zero. The await() method disabled the current thread for thread scheduling purposes when the current count is neither zero nor a negative number, and the current thread remains dormant until one of the following things happens:
Syntax: Parameters: timeout: The timeout parameter is of type long, which defines the maximum time to wait. unit: The unit parameter is of type TimeUnit, which defines the time unit of the timeout argument. Returns It returns true when the count reaches zero and returns false when the waiting time elapsed before the count reached zero. Throws The await() method throws interruptedException when the current thread is interrupted while waiting. 3. countdownThe countdown() method is another important method provided by the CountDownLatch class. It documents the count of the latch and releases all the waiting threads when the count reaches zero. It has done the following things:
4. getCountIt is another important method provided by the CoutDownLatch class. The getCount() method is used to get the count of the latch which we currently use. Syntax: It does not accept any parameter. It returns the current count of the latch. 5. toStringThe toString() is the last method provided by the CountDownLatch class. It is used to get a string that will identify the latch and its state. Syntax: Overrides It overrides the toString in class Object. Returns It returns the string that will identify the latch and its state. Let's take an example of the CountDownLatch and understand how it works and can implement in java. CountDownLatchExample.java Output: Next TopicCounting sort in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India