多线程计数器之CountDownLatch和join

[java]  view plain  copy
 print ?
  1. /** 
  2.  * 这个是实现计数器操作,主线程等待子线程全部完成 
  3.  * 模拟场景:10个人赛跑,当所有的人到达终点后,比赛结束 
  4.  * @author zhangm 
  5.  * 
  6.  */  
  7. public class TestCountDownLatch  {  
  8.   
  9.      //计数器,从2开始计数  
  10.      private final static CountDownLatch mCountDownLatch = new CountDownLatch(3);  
  11.        
  12.      /** 
  13.          * 示例工作线程类 
  14.          */  
  15.         private static class WorkingThread extends Thread {  
  16.             private final String mThreadName;  
  17.             private final int mSleepTime;  
  18.             public WorkingThread(String name, int sleepTime) {  
  19.                 mThreadName = name;  
  20.                 mSleepTime = sleepTime;  
  21.             }  
  22.               
  23.             @Override  
  24.             public void run() {  
  25.                 System.out.println("[" + mThreadName + "] started!");  
  26.                 try {    
  27.                         Thread.sleep(mSleepTime);    
  28.                 } catch (InterruptedException e) {    
  29.                         e.printStackTrace();    
  30.                 }  
  31.                 mCountDownLatch.countDown();  
  32.                 System.out.println("[" + mThreadName + "] end!");   
  33.             }  
  34.         }  
  35.           
  36.         /** 
  37.          * 示例线程类 
  38.          */  
  39.         private static class SampleThread extends Thread {  
  40.               
  41.             @Override  
  42.             public void run() {  
  43.                 System.out.println("[SampleThread] started!");  
  44.                 try {  
  45.                     // 会阻塞在这里等待 mCountDownLatch 里的count变为0;  
  46.                     // 也就是等待另外的WorkingThread调用countDown()  
  47.                     mCountDownLatch.await();  
  48.                 } catch (InterruptedException e) {  
  49.                       
  50.                 }  
  51.                 System.out.println("[SampleThread] end!");  
  52.             }  
  53.         }  
  54.           
  55.         public static void main(String[] args) throws Exception {  
  56.             // 最先run SampleThread  
  57.             new SampleThread().start();  
  58.             // 运行工作线程  
  59.             new WorkingThread("WorkingThread1"1000).start();  
  60.             new WorkingThread("WorkingThread2"2000).start();  
  61.             new WorkingThread("WorkingThread3"1000).start();  
  62.         }  
  63.       
  64. }   

执行结果如下:

[SampleThread] started!
[WorkingThread1] started!
[WorkingThread2] started!
[WorkingThread3] started!
[WorkingThread1] end!
[WorkingThread2] end!
[WorkingThread3] end!
[SampleThread] end!


另外还可以使用join使得主线程等待子线程完成。

[java]  view plain  copy
 print ?
  1. public class TestJoin extends Thread{  
  2.   
  3.     private  String name;  
  4.     private  int  time;  
  5.       
  6.     public TestJoin(String name,int time)  
  7.     {  
  8.         this.name=name;  
  9.         this.time=time;  
  10.     }  
  11.   
  12.     //子线程执行部分  
  13.     @Override  
  14.     public void run()  
  15.     {  
  16.         System.out.println(this.getName() + " staring...");  
  17.         try {    
  18.             Thread.sleep(time);    
  19.         } catch (InterruptedException e) {    
  20.                 e.printStackTrace();    
  21.         }  
  22.         System.out.println(this.getName() + " end...");  
  23.     }  
  24.   
  25.       
  26.     public static void main(String[] args)  
  27.     {  
  28.         System.out.println("main thread starting...");  
  29.   
  30.         List<TestJoin> list = new ArrayList<TestJoin>();  
  31.   
  32.         int time;  
  33.         for (int i = 1; i <= 5; i++)  
  34.         {  
  35.             time=(int) (Math.random()*3+1);  
  36.             TestJoin my = new TestJoin("Thrad " + i,1000*time);  
  37.             my.start();  
  38.             list.add(my);  
  39.         }  
  40.   
  41.         try  
  42.         {  
  43.             for (TestJoin my : list)  
  44.             {  
  45.                 my.join();  
  46.             }  
  47.         }  
  48.         catch (InterruptedException e)  
  49.         {  
  50.             e.printStackTrace();  
  51.         }  
  52.   
  53.         System.out.println("main thread end...");  
  54.   
  55.     }  
  56.   
  57. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值