ForkJoinPool 中的工作线程是守护线程吗?

Are worker threads in ForkJoinPool are Daemon threads?

我正在阅读书 Java - The Complete Reference 中有关 Fork/Join 框架的内容。它说 ForkJoinPool 使用守护进程线程:

ForkJoinPool uses daemon threads. A daemon thread is automatically terminated when all user threads have terminated. Thus, there is no need to explicitly shut down a ForkJoinPool. However, with the exception of the common pool, it is possible to do so by calling shutdown( ). The shutdown( ) method has no effect on the common pool.

  1. 这是否意味着所有 ForkJoinWorkerThread 都是守护线程?
  2. 既然守护线程是 low priority threads,那么我们不应该将 ForkJoinPool 用于重要的任务吗?
  3. 如果工作线程不是守护线程,那么 shutdown() 是否等待工作线程完成?
  1. 不是,daemon线程的优先级和普通线程是一样的。 此外,您可以将它们的优先级设置为所需的级别。 引用的文章只是建议将守护线程用于不太重要的任务,因为不能保证它们在 JVM 退出时完成工作。

1.

jshell> ForkJoinPool.commonPool().execute(() ->  System.out.println(Thread.currentThread().getClass() + " isDaemon?  " + Thread.currentThread().isDaemon()))
class java.util.concurrent.ForkJoinWorkerThread isDaemon?  true

jshell>

A: 是的,它们是守护线程。

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

A: 默认情况下,ForkJoinPool 创建与任何其他线程具有相同优先级的线程。

3.

来自 ForkJoinPool#commonPool 的 javadoc

Returns the common pool instance. This pool is statically constructed; its run state is unaffected by attempts to shutdown() or shutdownNow(). However this pool and any ongoing processing are automatically terminated upon program System.exit(int). Any program that relies on asynchronous task processing to complete before program termination should invoke commonPool().awaitQuiescence, before exit.

A: ForkJoinPool 忽略关闭,但应用程序可以调用 awaitQuiescence 以确保所有任务完成。