zoukankan      html  css  js  c++  java
  • 多个线程运行结束之后主线程再执行CountDownLatch

    多个线程运行结束之后主线程再执行CountDownLatch

    学习了:http://blog.csdn.net/lvyuanj/article/details/50737123  这个要膜拜一下!

    http://blog.csdn.net/lynnlovemin/article/details/75604209

    膜拜一下:  原文引用:

    import java.util.concurrent.CountDownLatch;  
      
    /** 
     * @filename      : MyThread.java 
     * @description   : 描述(中文) 
     * @author        : lvyuanjun 
     * @create        : 2016年2月24日 下午2:04:30  
     * 
     * Modification History:修改日志 
     * Date                 Author       Version               description 
     * ------------------------------------------------------------------------------- 
     * 2016年2月24日 下午2:04:30      lvyuanjun 
     */  
    public class MyThread {  
          
        public static void main(String[] args) throws InterruptedException {  
              
            CountDownLatch countDownLatch = new CountDownLatch(3); //子线程计算器  
              
            System.out.println("start...");  
              
            FirstThread f = new FirstThread(countDownLatch);  
            Thread thread = new Thread(f);  
            thread.start();  
              
            SecondThread s = new SecondThread(countDownLatch);  
            Thread thread1 = new Thread(s);  
            thread1.start();  
              
            ThreeThread t = new ThreeThread(countDownLatch);  
            Thread thread2 = new Thread(t);  
            thread2.start();  
              
            countDownLatch.await(); //等待子线程计算器为零时,则所有的子线程都已经运行完成  
              
            System.out.println("end...");  
        }  
    }  
      
      
      
      
    class FirstThread implements Runnable {  
          
        private CountDownLatch countDownLatch;  
          
        public FirstThread(CountDownLatch countDownLatch){  
            this.countDownLatch = countDownLatch;  
        }  
          
        public void run() {  
            System.out.println("FirstThread start....");  
            try {  
                Thread.sleep(1000*5);  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }finally{  
                System.out.println("FirstThread start1....");  
                countDownLatch.countDown(); //子线程执行完之后,子线程计算器减一,直至到零  
            }  
              
        }  
    }  
    class SecondThread implements Runnable{  
          
        private CountDownLatch countDownLatch;;  
          
        public SecondThread(CountDownLatch countDownLatch){  
            this.countDownLatch = countDownLatch;  
        }  
          
        @Override  
        public void run() {  
            System.out.println("SecondThread start....");  
            try {  
                Thread.sleep(1000*9);  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }finally{  
                System.out.println("SecondThread start1....");  
                countDownLatch.countDown(); //子线程执行完之后,子线程计算器减一,直至到零  
            }  
        }  
    }  
    class ThreeThread implements Runnable{  
          
        private CountDownLatch countDownLatch;;  
          
        public ThreeThread(CountDownLatch countDownLatch){  
            this.countDownLatch = countDownLatch;  
        }  
        @Override  
        public void run() {  
            System.out.println("ThreeThread start....");  
            try {  
                Thread.sleep(1000*3);  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }finally{  
                System.out.println("ThreeThread start1....");  
                countDownLatch.countDown();  //子线程执行完之后,子线程计算器减一,直至到零 
            }  
        }  
    }  
  • 相关阅读:
    父亲对子女的话
    开通博客
    在linux下安装MySQLdb及基本操作
    java 词汇表速查手册
    java数据源的几种配置
    DBCP的参数配置
    Linux crontab定时执行任务
    很好看的Button CSS.
    C# 创建活动目录.txt
    解密存储过程
  • 原文地址:https://www.cnblogs.com/stono/p/8337016.html
Copyright © 2011-2022 走看看