CountDownLatch详解

来源:互联网 发布:电脑编程入门自学黑客 编辑:程序博客网 时间:2024/06/14 23:29

前言:刚刚有个朋友在群里问我:如何在主线程开启多个子线程去执行多个job任务,任务执行完以后再执行主线程。这个问题一下子让我回忆起了之前用过的闭锁:CountDownLatch


Java5.0到现在也有一些年头了,当时引入了许多令人称道的新特性,当然也引入了并发包:java.util.concurrent。而CountDownLatch就是并发包下的一个比较重要的工具类了。它的主要作用是就是解决我的那个朋友问我的那个问题:它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行。CountDownLatch是通过一个计数器来实现的,计数器的初始值为线程的数量。每当一个线程完成了自己的任务后,调用countDown()方法,计数器的值就会减1。当计数器值到达0时,它表示所有的线程已经完成了任务,然后在闭锁上等待的线程就可以恢复执行任务。直接上代码:

import java.util.ArrayList;import java.util.List;import java.util.concurrent.CountDownLatch;import java.util.concurrent.Executor;import java.util.concurrent.Executors;/** * 闭锁 * @author LiuChengxiang * @time 2017年10月21日上午10:54:25 * */public class TestCountDownLatch {    public static void main(String[] args) throws Exception{        CountDownLatch latch = new CountDownLatch(3);        List<Runnable> threads = new ArrayList<Runnable>();        threads.add(new ChildThread1(latch));        threads.add(new ChildThread2(latch));        threads.add(new ChildThread3(latch));        Executor executor = Executors.newFixedThreadPool(threads.size());        int i = 1;        for(final Runnable c : threads){            System.out.println("子线程"+i+"开始!");            executor.execute(c);            i++;            Thread.sleep(1000);        }        System.out.println("主线程等待三个子线程!");        latch.await();        Thread.sleep(500);        System.out.println("主线程继续执行!");    }}class ChildThread1 implements Runnable {    private CountDownLatch latch;    public ChildThread1(CountDownLatch latch){        this.latch = latch;    }    @Override    public void run() {        try{            Thread.sleep(6000);        }catch (Exception e){            e.printStackTrace();        }finally {            if(latch != null) {                latch.countDown();                System.out.println("子线程1完成!");            }        }    }}class ChildThread2 implements Runnable {    private CountDownLatch latch;    public ChildThread2(CountDownLatch latch){        this.latch = latch;    }    @Override    public void run() {        try{            Thread.sleep(7000);        }catch (Exception e){            e.printStackTrace();        }finally {            if(latch != null) {                latch.countDown();                System.out.println("子线程2完成!");            }        }    }}class ChildThread3 implements Runnable {    private CountDownLatch latch;    public ChildThread3(CountDownLatch latch){        this.latch = latch;    }    @Override    public void run() {        try{            Thread.sleep(8000);        }catch (Exception e){            e.printStackTrace();        }finally {            if(latch != null) {                latch.countDown();                System.out.println("子线程3完成!");            }        }    }}/*    执行结果:    子线程1开始!    子线程2开始!    子线程3开始!    主线程等待三个子线程!    子线程1完成!    子线程2完成!    子线程3完成!    主线程继续执行!*/

今天就实现一个简单的Demo,让骚年们看清CountDownLatch到底做到了什么和基本用法,改天有时间博主会更新博客详细给大家讲解一下JavaSE并发包下一些主要的类和方法,包括AbstractQueuedSynchronizer,ReadWriteLock.class,CyclicBarrier等等。不墨迹了,博主还要撸代码。

原创粉丝点击