- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为了避免将自定义 ThreadFactory
传递给 ThreadPoolExecutor
以直接使用 Executors.newCachedThreadPool();
。
我创建了一个线程mainDaemonThread
,使用Executors.newCachedThreadPool();
,提交任务,在mainDaemonThread
启动之前,我设置了它daemon
据我所知,一旦 parent 线程是守护进程,那么它创建的所有线程默认都是 daemon
。
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/
Issue 2019/05/09 21:50:07.380 +0800 ERROR [ExecutorManager] [Azkaban] No active executors found
我的问题是:使用 Executors.newFixedThreadPool(1)?? 有意义吗? 。在两个线程(main + oneAnotherThread)场景下使用执行器服务是否高效?正在通过调
我想知道,Executors.newSingleThreadExecutor() 之间有什么区别?和 Executors.newFixedThreadPool(1) 以下摘自javadoc Unlik
我的问题是:使用 Executors.newFixedThreadPool(1) 有意义吗??。在两个线程(main + oneAnotherThread)场景中使用执行器服务是否有效?通过调用 ne
我有一个 Apache Spark 应用程序在集群模式下运行在 YARN 集群上(spark 在这个集群上有 3 个节点)。 当应用程序运行时,Spark-UI 显示 2 个执行程序(每个运行在不同的
我想知道是否有任何理由使用 Executor 而不是 ExecutorService。 据我所知,JDK 中没有实现 Executor 接口(interface),它也不是 ExecutorServi
我有多个使用 Celery Executor 的 dag,但我希望使用 Kubernetes Executor 运行一个特定的 dag。我无法推断出一种良好而可靠的方法来实现这一目标。 我有一个 ai
假设我们的 Controller 中有一个 Action 。在每次请求时,许多用户都会调用 performLogin。 def performLogin( ) = { Async {
创建和管理您自己的 ExecutorService 与使用 Spring Boot 的 @Async 方法和 @Bean 方法创建 Executor 添加一个@Bean来创建一个Executor 手动
问题从无到有,只有我在代码中所做的更改 - 安装了 RaSharper(但删除它并重新安装 Visual Studio 没有帮助)。 所以我使用 NUnit 3 来运行测试。 我有 Visual St
我们知道每个任务当时都在一个核心中执行。 假设我们有这样配置的节点集群: 10 节点。 每个节点 16 个核心。 每个节点 64 GB 内存。 我的问题是 有 1 个 16 核的执行程序和 16 个
我正在从 Jupyter Notebook 中初始化 PySpark,如下所示: from pyspark import SparkContext # conf = SparkConf().setAp
我正在向我的 Web 应用程序添加一个基于 Flask 的 API,以控制某些网络自动化功能的启动和停止。我遇到了一个奇怪的行为,即 Flask-Executor .submit() 方法调用的函数似
单元测试在本地运行良好。 在 Visual Studio 2017 托管生成代理上运行时,VSTest 任务失败并显示: 2018-12-08T10:42:16.3779907Z An excepti
我正在尝试制作一个执行器和线程的简单示例。 当我调用 newSingleThreadExecutor(new CustomThreadFactory) 时,一切顺利,但是当我使用 null 参数调用
对于一个线程,我通过以下代码段捕获未捕获的异常。但是,对于 ExecutorService executor = Executors.newFixedThreadPool(10);,如何捕获未捕获的异
我想创建一个 CompletableFuture,其返回值在 Kotlin 中的特定执行程序上运行。 下面的代码工作得很好。 return CompletableFuture.supplyAsync
考虑基本的固定线程池: Executors.newFixedThreadPool(MaxListeners) 我打算不断提交新任务 - 响应传入的 TCP 套接字服务请求。 然而,当每个任务中的Run
我们可以在定义 ThreadPoolExecutors 时提供 BlockingQueue 实现。但是,如果我使用工厂(执行器)创建单个线程池(如下所示),我想知道使用哪个阻塞队列。我猜它是一个 Li
我编写了一个程序来执行两个在 shell 前台运行的命令,直到在终端上按下 ^c。 外壳命令 ./weed master -mdir=/var/lib/qualebs/weed 上面命令的输出是 qu
我是一名优秀的程序员,十分优秀!