Android开发经验谈Android开发程序员

CountDownLatch 使用详解

2018-09-03  本文已影响18人  一团捞面

1. CountDownLatch 是什么?

从这个类的字面意思来入手,CountDown 是倒计时的意思,Latch 是门栓的意思,加起来的意思就是一个倒计时的门栓。

它其实是作用于线程当中的,它就像一个门栓,一开始是关闭的,所有希望通过该门的线程都需要等待,然后开始倒计时,当倒计时一到,等待的所有线程都可以通过。

要注意的是,它是一次性的,打开之后就不能关上了。

2. 怎么使用 CountDownLatch

2.1 构造方法

public CountDownLatch(int count)

解释:

2.2 重要方法

// 调用此方法的线程会被阻塞,直到 CountDownLatch 的 count 为 0
public void await() throws InterruptedException 

// 和上面的 await() 作用基本一致,只是可以设置一个最长等待时间
public boolean await(long timeout, TimeUnit unit) throws InterruptedException

// 会将 count 减 1,直至为 0
public void countDown() 

2.3 基本使用

2.3.1 需求

现在实现主线程等待其他线程的任务完成之后,才继续执行的代码。

2.3.2 代码实现

public class CountDownLatchDemo {
    
    static class TaskThread extends Thread {
        
        CountDownLatch latch;
        
        public TaskThread(CountDownLatch latch) {
            this.latch = latch;
        }
        
        @Override
        public void run() {
            
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println(getName() + " Task is Done");
                latch.countDown();
            }
            
            
        }
        
    }
    
    public static void main(String[] args) throws InterruptedException {
        
        int threadNum = 10;
        CountDownLatch latch = new CountDownLatch(threadNum);
        
        for(int i = 0; i < threadNum; i++) {
            TaskThread task = new TaskThread(latch);
            task.start();
        }
        
        System.out.println("Task Start!");
        
        latch.await();
        
        System.out.println("All Task is Done!");
    }

}

代码解释:

打印结果:

Task Start!
Thread-1 Task is Done
Thread-5 Task is Done
Thread-8 Task is Done
Thread-9 Task is Done
Thread-2 Task is Done
Thread-7 Task is Done
Thread-6 Task is Done
Thread-4 Task is Done
Thread-3 Task is Done
Thread-0 Task is Done
All Task is Done!

3. CountDownLatch 使用场景

3.1 同时启动多个线程

如果想同时启动多个线程,实现最大的并行性,就可以使用 CountDownLatch。稍微修改上面的例子代码,代码如下:

public class CountDownLatchDemo {
    
    static class TaskThread extends Thread {
        
        CountDownLatch latch;
        
        public TaskThread(CountDownLatch latch) {
            this.latch = latch;
        }
        
        @Override
        public void run() {
            
            try {
                latch.await();
                System.out.println(getName() + " start " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } 
        }
        
    }
    
    public static void main(String[] args) throws InterruptedException {
        
        int threadNum = 10;
        CountDownLatch latch = new CountDownLatch(1);
        
        for(int i = 0; i < threadNum; i++) {
            TaskThread task = new TaskThread(latch);
            task.start();
        }
        
        Thread.sleep(1000);
        latch.countDown();

    }

}

代码解释:

打印结果:

Thread-1 start 1535987605552
Thread-3 start 1535987605552
Thread-2 start 1535987605552
Thread-0 start 1535987605552
Thread-6 start 1535987605552
Thread-5 start 1535987605552
Thread-8 start 1535987605552
Thread-4 start 1535987605552
Thread-9 start 1535987605552
Thread-7 start 1535987605552

3.2 等待多个线程完成各自任务后再开始执行自己的任务

这个例子在 2.3 节已经说过了,这里就不再细说了。

参考文章:

上一篇下一篇

猜你喜欢

热点阅读