zoukankan      html  css  js  c++  java
  • CountDownLatch的简单使用

    感觉一个有点累赘的累,先看构造方法

    1:

    /**
     * Constructs a {@code CountDownLatch} initialized with the given count.
     *
     * @param count the number of times {@link #countDown} must be invoked
     *        before threads can pass through {@link #await}
     * @throws IllegalArgumentException if {@code count} is negative
     */
    public CountDownLatch(int count) {
        if (count < 0) throw new IllegalArgumentException("count < 0");
        this.sync = new Sync(count);
    }

    传入的值,并非线程的数量,而是调用的次数。

    2:

    public void await() throws InterruptedException {
        sync.acquireSharedInterruptibly(1);
    }

    方法await()在规定的次数完成之后,便会返回值,一般由主线程调用

    网上拷贝了一个简单的栗子:

    package TestDemo;
    import java.util.*;
    import java.util.concurrent.CountDownLatch;
    
    public class CountDownLatchTest{
        //用于聚合所有的统计指标
        private static Map map = new HashMap();
        //创建计数器,这里需要统计4个指标
        private static CountDownLatch countDownLatch = new CountDownLatch(4);
    
        public static void main(String[] args) {
            //记录开始时间
            long startTime = System.currentTimeMillis();
            Thread countUserThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("正在统计新增用户数量");
                        Thread.sleep(3000);//任务执行需要3秒
                        // map.put("userNumber", 1);//保存结果值
                        countDownLatch.countDown();//标记已经完成一个任务
                        System.out.println("统计新增用户数量完毕");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            Thread countOrderThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("正在统计订单数量");
                        Thread.sleep(3000);//任务执行需要3秒
                        map.put("countOrder", 2);//保存结果值
                        countDownLatch.countDown();//标记已经完成一个任务
                        System.out.println("统计订单数量完毕");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            Thread countGoodsThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("正在商品销量");
                        Thread.sleep(3000);//任务执行需要3秒
                        map.put("countGoods", 3);//保存结果值
                        countDownLatch.countDown();//标记已经完成一个任务
                        System.out.println("统计商品销量完毕");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            Thread countmoneyThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("正在总销售额");
                        Thread.sleep(3000);//任务执行需要3秒
                        map.put("countmoney", 4);//保存结果值
                        countDownLatch.countDown();//标记已经完成一个任务
                        System.out.println("统计销售额完毕");
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            });
            //启动子线程执行任务
            countUserThread.start();
            countGoodsThread.start();
            countOrderThread.start();
            countmoneyThread.start();
            try {
                //主线程等待所有统计指标执行完毕
                countDownLatch.await();
                long endTime = System.currentTimeMillis();//记录结束时间
                System.out.println("------统计指标全部完成--------");
                System.out.println("统计结果为:" + map.toString());
                System.out.println("任务总执行时间为" + (endTime - startTime) / 1000 + "秒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    如有错误,请邮件zs253499660@sina.com,如有更好的方法,可以推荐
  • 相关阅读:
    django orm(2)
    django orm操作(1)
    Django 模型层(2)
    Django 模型层(1)
    6 Django的视图层
    Django的模板层
    可视化工具Navicat的使用
    adminlte html汉化
    nodejs安装package
    jquery验证框架validate的使用
  • 原文地址:https://www.cnblogs.com/senjiang/p/13383874.html
Copyright © 2011-2022 走看看