gpt4 book ai didi

java - 运行的线程数

转载 作者:行者123 更新时间:2023-12-03 21:30:56 26 4
gpt4 key购买 nike

我对下面的代码很好奇。假设我有一个运行下面代码的 main() 方法。使用以下代码可以运行的最大线程数是多少?

ExecutorService ex= Executors.newFixedThreadPool(3);
for(int i = 1; i <= 5; i++) {
RunnableTask task = new RunnableTask(i, 3);
ex.submit(task);
}

答案会是 4 吗?由于我有一个主线程和另外 3 个线程,因为 ExecutorService 只允许最多运行 3 个线程?

最佳答案

据官方documentation .

If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.

关于java - 运行的线程数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43451829/

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