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

    xiaoxiao2023-09-27  168

    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

    分析:存在两个线程,主线程和子线程t 在主线程中,子线程t调用了join方法,主线程就会等待子线程t执行结束之后,才会继续执行 所以先输出2,再输出1

    最新回复(0)