Java并发工具类CountDownLatch

java的并发工具中有一个CountDownLatch类,用于多个线程等待同一个信号。
想象一个场景,多个线程在处理不同的任务,需要在所有的线程执行到某个阶段之后,执行某一个任务。比如软件使用多线程加载不同的模块,等全部加载完成再启动下一步操作。

看一下源码注释:

A synchronization aid that allows one or more threads to wait until a set of operations being performed 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 CyclicBarrier.

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 could pass.

大意是:

这个类是一个同步工具类,允许一个或者多个线程在一系列其他线程中的操作执行完成之前进行等待。

CountDownLatch使用一个计数值进行初始化。await方法在count值变为0之前会一直阻塞,之后所有阻塞的线程都被立即释放。countDown能使count值减一。这是个one-shot现象–count不能重置。如果想要重置count,考虑使用CyclicBarrier。

CountDownLatch是多功能的同步工具,有多重用途。CountDownLatch使用一个count计数值初始化,作为一个on/off latch或者gate:所有的线程都阻塞,直到gate被一个调用countDown的线程打开。CountDownLatch初始化到N,用于等待N个线程完成同一操作,或者相同的操作重复N次。

CountDownLatch的一个有用的属性是,它不需要所有调用countDown的线程等待count变成0,而是简单地阻止任何在所有线程通过之前想要越过await执行其他操作的线程。

举个例子:

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");
    }
}

打印结果:

thread 3
thread 2…
thread 1…
====after latch
====thread 3 end…
thread 2 end…

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页