Java多线程中关于join的问题?

代码一:

public static void main(String[] args) throws Exception {
    Thread thread = new Thread(() -> {      try {
        TimeUnit.SECONDS.sleep(10);
        System.out.println("thread-0 thread exit.");
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }, "thread-0");    thread.start();    thread.join();    System.out.println("main thread exit.");
  }

控制台(10s后打印并退出):

thread-0 thread exit.
main thread exit.

Process finished with exit code 0

代码二:

public static void main(String[] args) throws Exception {
    Thread.currentThread().join();
    System.out.println("main thread exit.");
  }

控制台(一直等待中):


问题:第一段代码是main线程阻塞在thread-0线程上,当thread-0执行完毕,main线程也同时退出,那第二段代码中main线程阻塞在哪个线程上呢,为什么没有退出?


米脂
浏览 772回答 2
2回答

回首忆惘然

main线程阻塞自己
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java