CountDownLatch使用

来源:互联网 发布:淘宝双查号是什么意思 编辑:程序博客网 时间:2024/06/05 00:41

闲话少说,直接上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...*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

观察运行结果!

0 0