代码一:
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
线程阻塞在哪个线程上呢,为什么没有退出?
回首忆惘然
相关分类