https://blog.csdn.net/jaryle/article/details/74905379

Java主线程等待所有子线程执行完毕在执行,这个需求其实我们在工作中经常会用到,比如用户下单一个产品,后台会做一系列的处理,为了提高效率,每个处理都可以用一个线程来执行,所有处理完成了之后才会返回给用户下单成功。

我们通过以下的几种方法来解决:一、用sleep方法,让主线程睡眠一段时间,当然这个睡眠时间是主观的时间,是我们自己定的,这个方法不推荐,但是在这里还是写一下,毕竟是解决方法

二、使用Thread的 join() 等待所有的子线程执行完毕,主线程在执行,thread.join()把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程。比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B。


/**
 * 
 * 处理一个业务逻辑的场景:当一组线程都执行完之后,在执行别的线程(后者要使用前者返回的结果)
 * @author Administrator
 *
 */
public class ThreadDemo {
 
	public static void main(String[] args) throws InterruptedException {
		Vector<Thread> vectors=new Vector<Thread>();
		//启用5个线程
		for(int i=1;i<=5;i++){
			Thread childrenThread=new Thread(new Runnable(){
				 public void run(){
					 try {
						Thread.sleep(1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
					 System.out.println("子线程执行!");
					 
				 }
			});
			vectors.add(childrenThread);
			childrenThread.start();
		}
		//主线程
		for(Thread thread : vectors){
			thread.join(); //使用join来保证childrenThread的5个线程都执行完后,才执行主线程
		}
		System.out.println("主线程执行!");
 
	}
 
}

三、 下面结合这个问题我介绍一些并发包里非常有用的并发工具类,等待多线程完成的

CountDownLatch

/**
 * 
 * 处理一个业务逻辑的场景:当一组线程都执行完之后,在执行别的线程(后者要使用前者返回的结果)
 * @author Administrator
 *
 */
public class ThreadDemo2 {
 
	public static void main(String[] args) throws InterruptedException {
		final CountDownLatch latch= new CountDownLatch(5);//使用java并发库concurrent
		//启用5个线程
		for(int i=1;i<=5;i++){
			new Thread(new Runnable(){
				 public void run(){
					 try {
						Thread.sleep(1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
					 System.out.println("子线程执行!");
					 latch.countDown();//让latch中的数值减一
					 
				 }
			}).start();
			
		}
		//主线程
		latch.await();//阻塞当前线程直到latch中数值为零才执行
		System.out.println("主线程执行!");
 
	}
 
}

在这里说明一点,countDownLatch不可能重新初始化或者修改CountDownLatch对象内部计数器的值,一个线程调用countdown方法happen-before另外一个线程调用await方法

四、同步屏障CyclicBarrier

/**
 * 
 * 处理一个业务逻辑的场景:当一组线程都执行完之后,在执行别的线程(后者要使用前者返回的结果)
 * @author Administrator
 *
 */
public class ThreadDemo3 {
 
	public static void main(String[] args) throws Exception {
		final CyclicBarrier barrier=new CyclicBarrier(5);
		//启用5个线程
		for(int i=1;i<=5;i++){
			new Thread(new Runnable(){
				 public void run(){
					 try {
						Thread.sleep(1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
					 System.out.println("子线程执行!");
					 try {
						barrier.await();//到达屏障
					} catch (InterruptedException | BrokenBarrierException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					 
				 }
			}).start();
			
		}
		//主线程
		barrier.await();//阻塞当前线程直到latch中数值为零才执行
		System.out.println("主线程执行!");
 
	}
 
}