Java笔试题学习之28(线程join方法)

public static void main(String[] args) throws InterruptedException { Thread t=new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("2"); } }); t.start(); t.join(); System.out.println("1"); }

输出:
2
1
【Java笔试题学习之28(线程join方法)】分析:存在两个线程,主线程和子线程t
在主线程中,子线程t调用了join方法,主线程就会等待子线程t执行结束之后,才会继续执行
所以先输出2,再输出1

    推荐阅读