深入理解JUC:第四章:CountDownLatch倒计时器

理论:

CountDownLatch是一个非常实用的多线程控制工具类。常用的就下面几个方法:

    CountDownLatch(int count) //实例化一个倒计数器,count指定计数个数
    countDown() // 计数减一
    await() //等待,当计数减到0时,所有线程并行执行

未使用CountDownLatch的代码:

    public class Demo {
     
        public static void main(String[] args) {
            for (int i = 0; i < 5; i++) {
                new Thread(()->{
                    System.out.println(Thread.currentThread().getName()+"\t 工作任务完成,离开公司");
                },String.valueOf(i)).start();
            }
            System.out.println(Thread.currentThread().getName()+"\t 最后将公司门锁死,离开公司");
        }
    }

控制台:

使用CountDownLatch的代码:

    public class Demo {
     
        public static void main(String[] args) throws Exception{
            CountDownLatch countDownLatch = new CountDownLatch(5);
            for (int i = 0; i < 5; i++) {
                new Thread(()->{
                    System.out.println(Thread.currentThread().getName()+"\t 工作任务完成,离开公司");
                    countDownLatch.countDown();
                },String.valueOf(i)).start();
            }
            countDownLatch.await();
            System.out.println(Thread.currentThread().getName()+"\t 最后将公司门锁死,离开公司");
        }
    }

控制台:

案例二:

    package tst.project;
     
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
     
    public class Test {
     
        public static void main(String[] args) {
            int tasks = 3;
            final CountDownLatch latch = new CountDownLatch(tasks);
            ExecutorService executorService = Executors.newFixedThreadPool(tasks);
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("5秒完成:"+latch);
                    latch.countDown();
                }
            });
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("3秒完成:"+latch);
                    latch.countDown();
                }
            });
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("1秒完成:"+latch);
                    latch.countDown();
                }
            });
            executorService.shutdown();
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

控制台:

 

结合下一章阅读:https://blog.csdn.net/java_wxid/article/details/99170471