zoukankan      html  css  js  c++  java
  • 等待多个并发事件完成的模型

    简单的一个常见问题:如下
    一个人刷牙3分钟,洗脸1分钟,梳头1分钟,煮鸡蛋5分钟。完成这些事情最少多少时间?
    这其实对应编程来说就对应了题目的问题了,如何让主线程计算出多个并发事件完成的时间问题了。
    我们下面会接触一个java类,那就是CountDownLatch 类,详细内容后面有时间详细添加,先下面给出一个案例代码。
    下面代码只创建2个任务线程,计算完成任务最少的时间。
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5
    package test;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.concurrent.CountDownLatch;
    
    public class Test {
        public static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        public static void main(String[] args) {
            long start =new Date().getTime();
            CountDownLatch latch=new CountDownLatch(2);//两个任务
            DoSomething DoSomething1=new DoSomething("刷牙", 3000, latch);//模拟3秒代表3分钟
            DoSomething DoSomething2=new DoSomething("煮鸡蛋", 5000, latch);//模拟5秒代表5分钟
            DoSomething1.start(); 
            DoSomething2.start(); 
            try {
                latch.await();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }//等待所有任务完成
            System.out.println("总用时: "+(new Date().getTime()-start)/1000 +"秒");
        }
    
    
       public static class DoSomething extends Thread{
            String jobName; 
            int needTime;
            CountDownLatch latch;
            public DoSomething(String jobName ,int needTime ,CountDownLatch latch){
                 this.jobName=jobName;
                 this.needTime=needTime;
                 this.latch=latch;
            }
            public void run(){
                System.out.println(sdf.format(new Date())+": "+jobName+"开始");
                try {
                    Thread.sleep(needTime);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    System.out.println(sdf.format(new Date())+": "+jobName+"结束");
                    latch.countDown();//一个任务完成
                }
            }
        }
    
    
    }
    
    • 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
    • 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

    这里写图片描述

    http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244545
    http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244541
    http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244538
    http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244527
    http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244528
    http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244529
    http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244530

  • 相关阅读:
    【数据结构】线性表&&顺序表详解和代码实例
    【智能算法】超详细的遗传算法(Genetic Algorithm)解析和TSP求解代码详解
    【智能算法】用模拟退火(SA, Simulated Annealing)算法解决旅行商问题 (TSP, Traveling Salesman Problem)
    【智能算法】迭代局部搜索(Iterated Local Search, ILS)详解
    10. js时间格式转换
    2. 解决svn working copy locked问题
    1. easyui tree 初始化的两种方式
    10. js截取最后一个斜杠后面的字符串
    2. apache整合tomcat部署集群
    1. apache如何启动
  • 原文地址:https://www.cnblogs.com/sy646et/p/7266031.html
Copyright © 2011-2022 走看看