Java concurrency tool CountDownLatch

Source: Internet
Author: User

Java concurrency tool CountDownLatch

Java's concurrency tool has a CountDownLatch class for multiple threads to wait for the same signal.
Imagine a scenario where multiple threads are processing different tasks and need to execute a task after all threads are executed to a certain stage. For example, the software uses multiple threads to load different modules and starts the next step after all the modules are loaded.

Let's take a look at the source code Annotations:

A synchronization aid that allows one or more threads to wait until a set of operations being stored med in other threads completes.

A CountDownLatch is initialized with a given count. the await methods block until the current count reaches zero due to invocations of the countDown method, after which all waiting threads are released and any subsequent invocations of await return immediately. this is a one-shot phenomenon-the count cannot be reset. if you need a version that resets the count, consider using a packet icbarrier.

A CountDownLatch is a versatile synchronization tool and can be used for a number of purposes. A CountDownLatch initialized with a count of one serves as a simple on/off latch, or gate: all threads invoking await wait at the gate until it is opened by a thread invoking countDown. A CountDownLatch initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times.

A useful property of a CountDownLatch is that it doesn't require that threads calling countDown wait for the count to reach zero before proceeding, it simply prevents any thread from proceeding past an await until all threads cocould pass.

The general idea is:

This class is a synchronization tool class that allows one or more threads to wait before a series of operations in other threads are completed.

CountDownLatch uses a Count value for initialization. The await method will be blocked until the count value is 0, and all blocked threads will be released immediately. CountDown can reduce the count value by one. This is a one-shot phenomenon.-count cannot be reset. If you want to reset the count, consider using javasicbarrier.

CountDownLatch is a versatile synchronization tool for multiple purposes. CountDownLatch is initialized using a count value as an on/off latch or gate: All threads are blocked until the gate is opened by a thread that calls countDown. CountDownLatch is initialized to N, which is used to wait for N threads to complete the same operation, or the same operation is repeated N times.

A useful attribute of CountDownLatch is that it does not need all threads that call countDown to wait until count becomes 0, but simply blocks any thread that wants to execute other operations over await before all threads pass through.

For example:

import java.util.concurrent.CountDownLatch;public class Main {    public static void main(String[] args) throws InterruptedException {    final CountDownLatch latch = new CountDownLatch(2);    Thread thread1 = new Thread(new Runnable() {        @Override        public void run() {        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(thread 1...);        latch.countDown();        }    });    Thread thread2 = new Thread(new Runnable() {        @Override        public void run() {        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(thread 2...);        latch.countDown();        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(thread 2 end...);        }    });    Thread thread3 = new Thread(new Runnable() {        @Override        public void run() {        System.out.println(thread 3);        try {            latch.await();        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(====thread 3 end...);        }    });    thread1.start();    thread2.start();    thread3.start();    latch.await();    System.out.println(====after latch);    }}

Print result:

Thread 3
Thread 2...
Thread 1...
==== After latch
==== Thread 3 end...
Thread 2 end...

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.