https://blog.csdn.net/AdminC/article/details/83929452

解法1. Thread.join方法

import java.util.Vector;

public class Test {
    public static void main(String[] args) throws InterruptedException {
    /*
    Vector集合: JDK1.0 底层也是数组  是最早期的同步集合 也就意味着是单线程 效率低
    			实现了list接口所以可以使用list的所有方法
      	特有方法:
      	 void addElement(E obj) 将指定的组件添加到此向量的末尾,将其大小增加 1
      	 Enumeration<E> elements() 返回此向量的组件的枚举(早期的迭代器)。
      */
        Vector<Thread> vector = new Vector<>();
        for (int i = 0; i < 5; i++) {
            Thread childThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("子线程被执行");
                }
            });
            vector.add(childThread);
            childThread.start();
        }
        for (Thread thread : vector) {
        // void join()  等待该线程终止
            thread.join();
        }
        System.out.println("主线程被执行");
    }
}

解法2.等待多线程完成的CountDownLatch

 public static void main(String[] args) throws InterruptedException {
        //创建CountDownLatch并初始化为5
        CountDownLatch latch = new CountDownLatch(5);
        for (int i = 0; i < 5; i++) {
            Thread childthread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("子线程开始执行");
                    //计数器递减,为0时释放所有线程
                    latch.countDown();
                }
            });
            childthread.start();
        }
        //设置主线程等待
        latch.await();
        System.out.println("主线程开始执行");
    }