CountDownLatch使用

来源:互联网 发布:linux使用教程.pdf 编辑:程序博客网 时间:2024/06/05 06:04

闲话少说,直接上demo

package com.ilucky.test.jdk.util.concurrent;import java.util.Date;import java.util.concurrent.CountDownLatch;/** * 一个线程(或者多个线程),等待另外n个线程完成某个事情之后执行. * CountDownLatch是计数器, 线程完成一个就记一个, 就像报数一样, 只不过是递减的. *  * CountDownLatch和CylicBarrier的区别: http://blog.csdn.net/kjfcpua/article/details/7300286 * @author IluckySi * */public class CountDownLatchTest {    public static void main(String[] args) {        CountDownLatch cdl = new CountDownLatch(3);        new Test1T1(cdl).start();        new Test1T2(cdl).start();        new Test1T3(cdl).start();        try {            cdl.await();            // 继续执行下面的逻辑...(略)        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println("Success...");    }}class Test1T1 extends Thread {    private CountDownLatch cdl;    Test1T1(CountDownLatch cdl) {        this.cdl = cdl;    }    public void run() {        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(new Date() + "name =" + Thread.currentThread().getName());        cdl.countDown();    }}class Test1T2 extends Thread {    private CountDownLatch cdl;    Test1T2(CountDownLatch cdl) {        this.cdl = cdl;    }    public void run() {        try {            Thread.sleep(2000);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(new Date() + "name =" + Thread.currentThread().getName());        cdl.countDown();    }}class Test1T3 extends Thread {    private CountDownLatch cdl;    Test1T3(CountDownLatch cdl) {        this.cdl = cdl;    }    public void run() {        try {            Thread.sleep(5000);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(new Date() + "name =" + Thread.currentThread().getName());        cdl.countDown();    }}/**Thu May 11 10:45:12 GMT+08:00 2017name =Thread-0Thu May 11 10:45:13 GMT+08:00 2017name =Thread-1Thu May 11 10:45:16 GMT+08:00 2017name =Thread-2Success...*/

观察运行结果!

0 0