gpt4 book ai didi

java - 使用 ThreadPoolExecutor,allowCoreThreadTimeOut 和零核心线程有什么区别?

转载 作者:行者123 更新时间:2023-11-30 07:41:33 27 4
gpt4 key购买 nike

阅读 ThreadPoolExecutor 的文档,我很困惑以下示例用法之间的区别是什么:

零个核心线程和十个最大线程,其中后者在 2 秒后超时:

ThreadPoolExecutor executor = new ThreadPoolExecutor(
0, // core threads
10, // max threads
2000, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
Executors.defaultThreadFactory()
);

10 个核心线程和 10 个最大线程均在 2 秒后超时:

ThreadPoolExecutor executor = new ThreadPoolExecutor(
10, // core threads
10, // max threads
2000, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
Executors.defaultThreadFactory()
);

executor.allowCoreThreadTimeOut(true);

这些执行者的行为是否有任何不同?

最佳答案

你可以阅读动机here .

I want something like fixed thread pool (Executors.newFixedThreadPool()) but with threads dying off when they are idle for too long, and re-created when they are needed again.

The most intuitive approach - set core size to 0, maximum pool size to the bounding value, and the queue to [an unbounded queue] - fails: no tasks get executed at all (consistently with Javadoc but - IMHO - a little bit counterintuitively).

executor.allowCoreThreadTimeOut(true); 可用于获得此行为。

如果指定了 corePoolSize = 0,为什么根本没有任务执行的更多细节

来自 Javadoc,

Using an unbounded queue (for example a LinkedBlockingQueue without a predefined capacity) will cause new tasks to wait in the queue when all corePoolSize threads are busy. Thus, no more than corePoolSize threads will ever be created. (And the value of the maximumPoolSize therefore doesn't have any effect.)

因此,如果您指定 corePoolSize = 0,则不会创建任何线程,也不会执行任何任务。

然而,实际上当 corePoolSize 为零时,实现会创建一个线程(已使用 Sun JDK6 和 OpenJDK11 进行测试)。因此在实践中,任务执行了,但是创建的线程不会超过一个,无论指定什么maximumPoolSize。我不完全确定为什么,因为根据规范,它甚至不应该创建一个。

下面是我的测试代码。这只会使用一个线程,但如果您指定 corePoolSize=10,它将使用 10。如果您添加 allowCoreThreadTimeOut(true),则这些线程可能会超时。

LinkedBlockingQueue<Runnable> q = new LinkedBlockingQueue<Runnable>();
ThreadPoolExecutor executor = new ThreadPoolExecutor(
0, // core threads
10, // max threads
2, TimeUnit.MILLISECONDS,
q
);

for (int i = 0; i < 10; i++) {
final int j = i;
executor.execute(() ->
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"TaskNo:" + j + ":" + Thread.currentThread().getName()
);
});
}

executor.shutdown();

关于java - 使用 ThreadPoolExecutor,allowCoreThreadTimeOut 和零核心线程有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55879100/

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