Table of Contents
1.CountDownLatch source code interpretation
2. Analysis of the principle of CountDownLatch
3. Application scenarios of CountDownLatch
Home Java javaTutorial How to use CountDownLatch class from Java Concurrency Toolkit?

How to use CountDownLatch class from Java Concurrency Toolkit?

May 09, 2023 pm 06:22 PM
java countdownlatch

CountDownLatch is a very practical tool class in the Java concurrency package, which can help us achieve synchronization and collaboration between threads. The core idea of ​​CountDownLatch is to control the execution order of threads through a counter. When the counter value drops to 0, all waiting threads will be awakened and then begin to perform the next operation.

1.CountDownLatch source code interpretation

In Java, the implementation of CountDownLatch is based on the AbstractQueuedSynchronizer class. AbstractQueuedSynchronizer is a very important synchronizer. Many concurrency classes in Java are implemented based on it, such as Semaphore, ReentrantLock, ReadWriteLock, etc.

The core implementation class of CountDownLatch is Sync, which is an internal class inherited from AbstractQueuedSynchronizer. The following is the source code of the Sync class:

private static final class Sync extends AbstractQueuedSynchronizer {
    Sync(int count) {
        setState(count);
    }

    int getCount() {
        return getState();
    }

    protected int tryAcquireShared(int acquires) {
        return (getState() == 0) ? 1 : -1;
    }

    protected boolean tryReleaseShared(int releases) {
        for (;;) {
            int c = getState();
            if (c == 0)
                return false;
            int nextc = c-1;
            if (compareAndSetState(c, nextc))
                return nextc == 0;
        }
    }
}
Copy after login

There are three important methods in the Sync class:

  • tryAcquireShared(int acquires): Try to acquire the lock, if the value of the counter is equal to 0, indicating that all threads have completed execution, returns 1, otherwise -1 is returned, indicating failure to acquire the lock.

  • tryReleaseShared(int releases): Release the lock, decrement the counter value by 1, and return the counter value after decrementing 1. If the value of the counter decreases to 0, it means that all threads have completed execution and returns true, otherwise it returns false.

  • getCount(): Returns the current counter value.

The tryAcquireShared() method is the key to CountDownLatch, it will try to acquire the lock. If the value of the counter is equal to 0, it means that all threads have completed execution, and 1 can be returned, indicating that the lock acquisition was successful; otherwise -1 is returned, indicating that the lock acquisition failed. The basic method of the AbstractQueuedSynchronizer class is used here, namely the getState() method, which is used to obtain the status of the synchronizer.

The tryReleaseShared() method is used to release the lock, decrement the counter value by 1, and return the counter value after decrementing 1. If the value of the counter decreases to 0, it means that all threads have completed execution and returns true, otherwise it returns false. The basic method of the AtomicInteger class is used here, namely the compareAndSetState() method, which is used to compare and set the status of the synchronizer.

2. Analysis of the principle of CountDownLatch

The working principle of CountDownLatch is very simple. It controls the execution order of threads through a counter. When the counter value drops to 0, all waiting threads will be awakened and then begin to perform the next operation.

CountDownLatch is a multi-thread collaboration tool class that allows one or more threads to wait for other threads to complete an operation before continuing. CountDownLatch has a counter. When the counter value becomes 0, the waiting thread will be awakened. The use of CountDownLatch is very simple, mainly including two methods: await() and countDown().

  • await() method: This method blocks the current thread until the counter value becomes 0.

  • countDown() method: This method will decrement the counter value by 1.

The following is a simple sample code:

public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        final int count = 3;
        final CountDownLatch latch = new CountDownLatch(count);

        for (int i = 0; i < count; i++) {
            new Thread(() -> {
                // 线程执行任务
                System.out.println(Thread.currentThread().getName() + " 执行任务...");
                // 任务执行完毕,计数器减1
                latch.countDown();
            }).start();
        }

        // 等待所有任务执行完毕
        latch.await();
        System.out.println("所有任务执行完毕...");
    }
}
Copy after login

In this sample code, we create a CountDownLatch object and initialize the counter to 3. Then 3 threads are created, each thread performs a task. After the task is completed, the counter is decremented by 1. Finally, call the latch.await() method in the main thread to wait for all tasks to be completed.

The implementation principle of CountDownLatch is based on the AbstractQueuedSynchronizer class. When we call the await() method, the thread will try to acquire the lock. If the counter value is not 0, the lock acquisition fails and the thread will be added to the synchronization queue and blocked. When we call the countDown() method, the counter value will be reduced by 1. If the counter value is reduced to 0, it means that all threads have completed execution. At this time, the threads in the synchronization queue will be awakened and continue to perform the next operation.

Specifically, in the Sync class, the tryAcquireShared(int acquires) method will try to acquire the lock. If the value of the counter is equal to 0, it means that all threads have completed execution and returns 1. Otherwise, it returns -1, which means Failed to acquire lock. The tryReleaseShared(int releases) method is used to release the lock, decrement the counter value by 1, and return the counter value after decrementing 1. If the value of the counter decreases to 0, it means that all threads have completed execution and returns true, otherwise it returns false.

3. Application scenarios of CountDownLatch

CountDownLatch is a very practical tool class that can help us achieve synchronization and collaboration between threads. The following introduces some common application scenarios of CountDownLatch:

  • Waiting for multiple threads to complete: If there are multiple threads that need to be executed, but you must wait for all threads to complete before proceeding to the next step. This can be achieved using CountDownLatch. We can create a CountDownLatch object and initialize the counter value to the number of threads. After each thread completes execution, call the countDown() method to decrement the counter by 1. Finally, call the await() method in the main thread to wait for all threads to complete execution.

  • Control the execution order of threads: If there are multiple threads that need to be executed in a specific order, you can use CountDownLatch to achieve this. We can create multiple CountDownLatch objects, and the counter value of each object is 1, indicating that only one thread can execute. After the thread completes execution, call the countDown() method of the next CountDownLatch object to wake up the next thread.

  • Waiting for the occurrence of external events: If we need to wait for the occurrence of an external event, such as the establishment of a network connection or the completion of reading a file, we can use CountDownLatch to achieve this. We can create a CountDownLatch object in the main thread, initialize the counter value to 1, and then wait for the external event to occur in another thread. When an external event occurs, call the countDown() method of the CountDownLatch object to wake up the main thread to continue execution.

  • Control the number of concurrent threads: If we need to control the number of concurrent threads, we can use CountDownLatch to achieve it. We can create a CountDownLatch object and initialize the counter value to the number of threads. After each thread completes execution, call the countDown() method to decrement the counter by 1. If a thread needs to wait for other threads to complete execution, it can call the await() method to wait for the counter value to become 0.

The above is the detailed content of How to use CountDownLatch class from Java Concurrency Toolkit?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles