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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值