Java多线程--主线程等待所有子线程执行完毕

来源:互联网 发布:windows壁纸 编辑:程序博客网 时间:2024/04/28 03:25

参考: http://blog.csdn.net/nms312/article/details/30115055

本文介绍两种主线程等待子线程的实现方式:

1、使用Thread的join()方法,join()方法会阻塞主线程继续向下执行。

2、使用java.util.concurrent中的CountDownLatch,是一个倒数计数器。初始化时先设置一个倒数计数初始值,每调用一次countDown()方法,倒数值减一,他的await()方法会阻塞当前进程,直到倒数至0。

public class Threads { public  static  void  main (String[]  args ) {        SubThread thread  =  new  SubThread () ;        thread.start () ;        //主线程处理其他工作,让子线程异步去执行.         mainThreadOtherWork () ;         System.out.println ( "now waiting sub thread done." ) ;        //主线程其他工作完毕,等待子线程的结束, 调用join系列的方法即可(可以设置超时时间)         try  {            thread.join () ;        }  catch  ( InterruptedException e) {            e.printStackTrace () ;        }        System.out.println ("now all done.") ;}private  static  void  mainThreadOtherWork () {         System.out.println ("main thread work start") ;          try  {              Thread.sleep ( 3000L ) ;         }  catch  ( InterruptedException e) {             e.printStackTrace () ;         }          System.out.println ("main thread work done.") ;    }}

package classstudy;public class  SubThread  extends  Thread{    @Override    public  void  run () {        working () ;   }    private  void  working () {        System.out.println ("sub thread start working." ) ;        busy () ;        System.out.println ("sub thread stop working." ) ;   }    private  void  busy () {        try  {             sleep ( 5000L ) ;       }  catch  ( InterruptedException e) {            e.printStackTrace () ;       }   }   }
运行结果:

main thread work start
sub thread start working.
main thread work done.
now waiting sub thread done.
sub thread stop working.
now all done.

package classstudy;import java.util.concurrent.CountDownLatch;  public class Thread1 extends Thread  {        private CountDownLatch count;        public Thread1(CountDownLatch count,String name)      {          this.count = count;          this.setName(name);      }        @Override      public void run()      {          System.out.println(this.getName() + " staring...");            System.out.println(this.getName() + " end...");          this.count.countDown();      }        /**      * @param args      */      public static void main(String[] args)      {          System.out.println("main thread starting...");            CountDownLatch count = new CountDownLatch(5);            for (int i = 1; i <= 5; i++)          {          Thread1 my = new Thread1(count, "Thread " + i);              my.start();          }            try          {              count.await();          }          catch (InterruptedException e)          {              e.printStackTrace();          }            System.out.println("main thread end...");        }    }  
运行结果:

main thread starting...
Thread 1 staring...
Thread 1 end...
Thread 3 staring...
Thread 3 end...
Thread 2 staring...
Thread 2 end...
Thread 4 staring...
Thread 4 end...
Thread 5 staring...
Thread 5 end...
main thread end...

实际应用场景,Map<String> myDynamicData, 这个动态变化的数据消失发送邮件。这样会存在一个问题,主线程等待子线程执行完毕,如果子线程耗费时间很长,将阻碍主线程继续向下执行,直观现象就是一直卡在那儿。

解决办法:启一个线程去监控myDynamicData里面的数据。


0 0
原创粉丝点击