gpt4 book ai didi

java - Executors.newCacheThreadPool 创建的线程不是守护进程,尽管池是在守护进程中创建的

转载 作者:行者123 更新时间:2023-11-30 06:44:10 25 4
gpt4 key购买 nike

为了避免将自定义 ThreadFactory 传递给 ThreadPoolExecutor 以直接使用 Executors.newCachedThreadPool();

我创建了一个线程mainDaemonThread,使用Executors.newCachedThreadPool();,提交任务,在mainDaemonThread启动之前,我设置了它daemon 据我所知,一旦 parent 线程是守护进程,那么它创建的所有线程默认都是 daemon

Daemon Thread

When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

那么为什么 Executors.newCachedThreadPool(); 不遵守规则?一些与此相关的设计偏好?

import static java.lang.System.out;
public static void main(String... args) throws InterruptedException {
Thread mainDaemonThread = new Thread(() -> {
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.submit(() -> {
try {
out.println(Thread.currentThread().isDaemon());
Thread.sleep(1000);
out.println(Thread.currentThread().isDaemon());
} catch(InterruptedException ignored) {
ignored.printStackTrace();
}
});
});
mainDaemonThread.setDaemon(true);
mainDaemonThread.start();
mainDaemonThread.join();
out.println(Thread.currentThread().isDaemon());
}

演示的输出:

false
false
false

任何帮助将不胜感激,谢谢~

最佳答案

我想这并不是一个正确的答案,但是:

您链接的文档是针对 Thread 类的,并记录了“手动”创建新 Thread 的行为。它只是不适用于您提交给 ExecutorService 的任务(尽管我明白您为什么会这样期望)。

如果您查看源代码,newCachedThreadPool 执行器使用一个(内部)DefaultThreadFactory,它显式创建一个非守护程序Thread:

public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false); // ta-da
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}

如果您希望执行程序创建一个守护进程线程,您可以使用 Executors#newCachedThreadPool(ThreadFactory) 方法和一个创建守护进程 Thread 的工厂。由于 ThreadFactory 是一个函数式接口(interface),这很简单

Executors#newCachedThreadPool(Thread::new);

关于java - Executors.newCacheThreadPool 创建的线程不是守护进程,尽管池是在守护进程中创建的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51076045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com