2014-05-16发表2023-01-13更新程序员1 分钟读完 (大约148个字)Java主线程等待子线程完成123456789101112131415161718192021222324252627282930313233343536373839404142434445464748import static java.lang.Thread.sleep;/** * * @author Gao Youbo * @since 2014-05-16 10:20:08 */public class Test { public static void main(String[] args) { SubThread thread = new SubThread(); thread.start(); //子线程开始 mainThreadWorking();//主线程干活 System.out.println("main:等待子线程完成"); try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("所有线程都完成!"); } private static void mainThreadWorking() { System.out.println("main:主线程开始干活..."); try { Thread.sleep(2000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("main:主线程干完了!"); }}class SubThread extends Thread { @Override public void run() { try { System.out.println("sub:子线程开始干活..."); sleep(6000L); //子线程花6秒钟时间干活 System.out.println("sub:子线程干完了!"); } catch (InterruptedException ex) { ex.printStackTrace(); } }}Java主线程等待子线程完成http://www.mspring.org/2014/05/16/Java主线程等待子线程完成/作者雾非雾的情思发布于2014-05-16更新于2023-01-13许可协议#Java