java - ForkJoinPool中的辅助线程是守护程序线程吗?

标签 java multithreading concurrency forkjoinpool

我正在从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都是守护程序线程?
  • 由于守护进程线程是low priority threads,那么我们不应该将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/

    相关文章:

    python WSME 和线程安全

    java - 程序顺序规则在构造函数中起作用之前是否发生?

    multithreading - 并行过滤惰性序列

    java - 除了 Collections.unmodifyingList() 之外,还有哪些方法可以使 arraylist 变为只读?

    java - 客户端日期时间与服务器日期时间不匹配

    java - 如何判断是否需要用@Bean进行注解

    c++ - Qt 在第一个窗口关闭时打开另一个窗口

    c# - 新线程导致 "Method Name Expected"错误

    java - java中的volatile变量和内存屏障

    java - jsp中的el表达式:invoke