gpt4 book ai didi

java - 空闲线程是否占用 Java 执行器中的 CPU 执行时间?

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

当我在应用程序中有这段代码时:

Executors.newFixedThreadPool(4);

但是我从没用过这个线程池。空闲线程会消耗CPU时间吗?如果是这样 - 为什么?

最佳答案

不,这些线程是延迟创建的,或者是“按需”创建的。如 the documentation 中所述(强调我的):

On-demand construction

By default, even core threads are initially created and started only when new tasks arrive

Java 提供了方法来覆盖此默认值并允许预先创建,即 prestartCoreThreadprestartAllCoreThreads .


一旦线程被实际创建,空闲线程(通常)不会占用 CPU 时间,因为当它们无事可做时没有理由将它们安排在核心上。

然而,他们仍然会保留一些内存,用于他们的堆栈和诸如此类的东西。

关于java - 空闲线程是否占用 Java 执行器中的 CPU 执行时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43795545/

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