• 深入理解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()+"	 工作任务完成,离开公司");
                },String.valueOf(i)).start();
            }
            System.out.println(Thread.currentThread().getName()+"	 最后将公司门锁死,离开公司");
        }
    }

    控制台:

    使用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()+"	 工作任务完成,离开公司");
                    countDownLatch.countDown();
                },String.valueOf(i)).start();
            }
            countDownLatch.await();
            System.out.println(Thread.currentThread().getName()+"	 最后将公司门锁死,离开公司");
        }
    }

    控制台:

    案例二:

    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

  • 相关阅读:
    人工智能发展与模型定制化趋势
    Merry Christmas-集成华为HMS ML Kit手部关键点识别来接住圣诞老人的礼物吧!
    快速构建ML Kit自定义模型,实现特定领域图像/文本分类
    华为consumeOwnedPurchase消耗接口返回-1错误码
    超简单集成华为恶意URL检测,保障用户访问安全
    深入解析 C# 的 String.Create 方法
    .NET 内存泄漏的争议
    Centos相关介绍 (连接别人)
    阿里云 Nginx.conf文件配置详解
    阿里云使用Nginx部署vue项目
  • 原文地址:https://www.cnblogs.com/javawxid/p/12811908.html
Copyright © 2020-2023  润新知