我正在阅读《Java - 完整参考》一书中有关 Fork/Join 框架的内容。它说 ForkJoinPool
使用守护线程 :
这是否意味着所有
ForkJoinWorkerThread
ForkJoinPool
来执行重要任务吗?如果工作线程不是守护线程,那么 shutdown()
jshell> ForkJoinPool.commonPool().execute(() -> System.out.println(Thread.currentThread().getClass() + " isDaemon? " + Thread.currentThread().isDaemon()))
class java.util.concurrent.ForkJoinWorkerThread isDaemon? true
jshell>
:是的,它们是守护线程。
2.
jshell> new Thread(() -> System.out.println("isDaemon? " + Thread.currentThread().isDaemon() + " priority:" + Thread.currentThread().getPriority())).start()
isDaemon? false priority:5
jshell> ForkJoinPool.commonPool().execute(() -> System.out.println("isDaemon? " + Thread.currentThread().isDaemon() + " priority:" + Thread.currentThread().getPriority()))
isDaemon? true priority:5
:ForkJoinPool 默认情况下会创建与任何其他线程具有相同优先级的线程。
3.来自 ForkJoinPool#commonPool 的 javadoc
返回公共池实例。该池是静态构建的;它的运行状态不受尝试 shutdown() 或 shutdownNow() 的影响。然而,该池和任何正在进行的处理都会在程序 System.exit(int) 时自动终止。任何依赖异步任务处理在程序终止之前完成的程序都应该在退出之前调用 commonPool().awaitQuithesis。
A
:ForkJoinPool 忽略关闭,但应用程序可以调用 awaitQuithesis 来确保所有任务都完成。
,“所有工作线程都通过 Thread.isDaemon()
设置为 true 进行初始化”。