gpt4 book ai didi

java - ExecutorService 可能在调用线程上执行?

转载 作者:搜寻专家 更新时间:2023-11-01 07:47:50 25 4
gpt4 key购买 nike

如果您阅读 Executor.execute 的文档,它说:

The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.

我的理解是,通过调用线程,它们意味着调用执行 命令的线程。

这在我的经验中从未发生过,所以我的问题是:这真的发生过吗?

由于调用 Thread 控制着 UI,我不能允许在调用 Thread 上进行额外的工作。我特别担心当我使用 Executors.newFixedThreadPool(threads) 方法构造 ExecutorService 时是否会发生这种情况。

最佳答案

ExecutorService 是一个定义广泛契约的接口(interface)。如果您查看实现,请说 ThreadPoolExecutor例如,您将具有更精确的特征:

An ExecutorService that executes each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods.

请注意,上面链接的 javadoc 详细说明了一个事实,即由各种 Executors 工厂方法返回的 ExecutorServiceThreadPoolExecutor

因此,更直接地回答您的问题:Executors.newFixedThreadPool(threads) 不会在调用线程上执行任务。

关于java - ExecutorService 可能在调用线程上执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40280998/

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