我正在从Java - The Complete Reference书中阅读有关Fork/Join Framework的信息。它说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.
ForkJoinWorkerThread
都是守护程序线程? ForkJoinPool
用于重要任务吗? shutdown()
是否等待工作线程完成? 最佳答案
1.
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
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.
:ForkJoinPool忽略关闭,但是应用程序可以调用 awaitQuiescence 以确保所有任务均已完成。
关于java - ForkJoinPool中的辅助线程是守护程序线程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63029461/