Loading

多个线程运行结束之后主线程再执行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();  //子线程执行完之后,子线程计算器减一,直至到零 
        }  
    }  
}  

 

posted @ 2018-01-23 17:37  stono  阅读(210)  评论(0编辑  收藏  举报