代码一: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线程阻塞在哪个线程上呢,为什么没有退出?
添加回答
举报
0/150
提交
取消