美文网首页
CountDownLatch用法

CountDownLatch用法

作者: endlesswork | 来源:发表于2019-10-04 23:28 被阅读0次

    CountDownLatch可以理解为是一个计数器。
    关于CountDownLatch类方法如下

    //构造实现类,count代表计数器大小
    public CountDownLatch(int count);
    //线程挂起等待计数器为0
    public void await();
    //线程在有限时间内挂起等待计数器为0
    public boolean await(long timeout, TimeUnit unit);
    //计数器减一
    public void countDown();
    

    CountDownLatch底层也是通过AQS实现,我们可以把我们定义的计数器大小理解为我们同时加了几把锁,countDown每次释放掉一把锁。

    例1

    public class CountDownLatchDemo {
    
        public static CountDownLatch countDownLatch = new CountDownLatch(1);
    
        static class ThreadDemo implements Runnable{
            public String name;
    
            public ThreadDemo(String name){
                this.name = name;
            }
    
    
            @Override
            public void run() {
                System.out.println(name+"开始"+getDate());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
                System.out.println(name+"结束"+getDate());
            }
        }
    
        static class ThreadDemo1 implements Runnable{
            public String name;
    
            public ThreadDemo1(String name){
                this.name = name;
            }
    
            @Override
            public void run() {
                System.out.println(name+"开始"+getDate());
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(name+"结束"+getDate());
            }
        }
    
        public static String getDate(){
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            return  dateFormat.format(date);
        }
        public static void main(String []args) {
            Thread thread1 = new Thread(new ThreadDemo("t1"));
            Thread thread2 = new Thread(new ThreadDemo1("t2"));
            thread1.start();
            thread2.start();
        }
    }
    

    结果如下,因为t2执行了await,所以它会等待t1执行完countDown再往下执行。

    t2开始2019-11-17 :11:30:26
    t1开始2019-11-17 :11:30:26
    t1结束2019-11-17 :11:30:28
    t2结束2019-11-17 :11:30:28
    

    例2

    public class CountDownLatchDemo2 {
    
        public static CountDownLatch countDownLatch = new CountDownLatch(1);
    
        static class ThreadDemo implements Runnable{
            public String name;
    
            public ThreadDemo(String name){
                this.name = name;
            }
    
            @Override
            public void run() {
                System.out.println(name+"开始"+getDate());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
                System.out.println(name+"结束"+getDate());
            }
        }
    
        static class ThreadDemo1 implements Runnable{
            public String name;
    
            public ThreadDemo1(String name){
                this.name = name;
            }
    
            @Override
            public void run() {
                System.out.println(name+"开始"+getDate());
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(name+"结束"+getDate());
            }
        }
    
        public static String getDate(){
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            return  dateFormat.format(date);
        }
        public static void main(String []args) {
            Thread thread1 = new Thread(new ThreadDemo("t1"));
            Thread thread2 = new Thread(new ThreadDemo1("t2"));
            Thread thread3 = new Thread(new ThreadDemo1("t3"));
            thread1.start();
            thread2.start();
            thread3.start();
        }
    }
    

    结果如下,可以看到我们启用2个线程await,也是会等待countDown完毕才会执行。

    t2开始2019-11-17 :11:43:58
    t1开始2019-11-17 :11:43:58
    t3开始2019-11-17 :11:43:58
    t1结束2019-11-17 :11:44:00
    t3结束2019-11-17 :11:44:00
    t2结束2019-11-17 :11:44:00
    

    例3

    public class CountDownLatchDemo3 {
    
        public static CountDownLatch countDownLatch = new CountDownLatch(1);
    
        static class ThreadDemo implements Runnable{
            public String name;
    
            public ThreadDemo(String name){
                this.name = name;
            }
    
            @Override
            public void run() {
                System.out.println(name+"开始"+getDate());
                countDownLatch.countDown();
                countDownLatch.countDown();
                System.out.println(name+"结束"+getDate());
            }
        }
    
        public static String getDate(){
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            return  dateFormat.format(date);
        }
        public static void main(String []args) {
            Thread thread1 = new Thread(new ThreadDemo("t1"));
            thread1.start();
        }
    }
    

    可以看到可以正常执行的

    t1开始2019-11-17 :11:49:03
    t1结束2019-11-17 :11:49:04
    

    例4

    public class CountDownLatchDemo4 {
    
        public static CountDownLatch countDownLatch = new CountDownLatch(1);
    
        static class ThreadDemo implements Runnable{
            public String name;
    
            public ThreadDemo(String name){
                this.name = name;
            }
    
            @Override
            public void run() {
                System.out.println(name+"开始"+getDate());
                countDownLatch.countDown();
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
                System.out.println(name+"结束"+getDate());
            }
        }
    
        public static String getDate(){
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            return  dateFormat.format(date);
        }
        public static void main(String []args) {
            Thread thread1 = new Thread(new ThreadDemo("t1"));
            thread1.start();
        }
    }
    

    可以看到例4也可以正常执行的

    t1开始2019-11-17 :11:51:07
    t1结束2019-11-17 :11:51:07
    

    相关文章

      网友评论

          本文标题:CountDownLatch用法

          本文链接:https://www.haomeiwen.com/subject/scwtuctx.html