threadpoolexcecutor的线程池中很少有线程主动执行任务

发布于 2024-11-02 17:56:40 字数 692 浏览 0 评论 0原文

我使用的是 ThreadPoolExecutorcorePoolSize = maxPoolSize = queueSize = 15

Every传入请求会生成 7 个任务,并使用该线程池执行。

尽管每个单独的任务在计划时只需要不到 3 秒的时间,但整个请求的时间却要长得多。

我怀疑系统缺少排队的线程和任务。

我为每个传入请求记录了以下信息。

getActiveCount()
getLargestPoolSize()
getPoolSize()
getQueue().size()

我注意到系统并没有缺少线程。

getPoolSizegetLargestPoolSize 值始终为 15 - 这是预期的。

getQueue().size() 始终为 0 - 因此没有任务排队。

getActiveCount() 值始终在 1-2 之间。

为什么池中的其余线程不工作?

是“getActiveCount() - 返回正在主动执行任务的线程的大致数量。”使用正确的 API 吗?

I am using a ThreadPoolExecutor with a corePoolSize = maxPoolSize = queueSize = 15

Every incoming request spawns 7 tasks, to be executed with this thread pool.

Even though each of the individual tasks, on being scheduled, take less than 3 seconds, the overall request takes much longer.

I suspected that the system is falling short of threads and tasks being queued.

I logged the following information for every incoming request.

getActiveCount()
getLargestPoolSize()
getPoolSize()
getQueue().size()

I notice that the system is not falling short of threads.

getPoolSize and getLargestPoolSize values are constantly at 15 - This is as expected.

getQueue().size() is always 0 - so no tasks are getting queued.

getActiveCount() values are always between 1-2.

Why aren't the rest of the threads in the pool working ?

Is "getActiveCount()-Returns the approximate number of threads that are actively executing tasks." the right API to use ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反话 2024-11-09 17:56:40

正如 @Thomas 所建议的,池根据需要创建线程,因此如果您只给池一次执行 1-2 个任务,那么它只会有 1-2 个线程处于活动状态。如果你想让它更忙,你需要一次向它提供更多任务。

As @Thomas suggests, the pool is creating threads as required so if you only give the pool 1-2 tasks to do at once, it will only have 1-2 threads active. You need to feed more tasks to it at once if you want it to be busier.

一生独一 2024-11-09 17:56:40

我不太了解 Java 的线程池,但通常您只会创建与您的机器具有可用内核(或硬件线程)一样多的线程。如果您在双核计算机上运行,​​则 2 个活动线程是一个合理的值。

I don't know Java's thread pool that well but generally you'd only create as many threads as your machine has cores (or hardware threads) available. If you are running on a dual core machine 2 active threads are a sensible value.

迷途知返 2024-11-09 17:56:40

您是否尝试过在 Eclipse 中以调试模式运行它?它通常显示分配的所有线程及其状态。
我怀疑在您的情况下会发生以下情况:您有线程池,并且线程池已分配了一定数量的线程来处理您提交到其中的任务。但是,一旦线程池中的任何特定线程完成了提交到线程池中的任务,它就不会终止。相反,它将其状态切换为“BLOCKED”或“WAITING”(我不记得具体是哪一个),并等待下一个任务传递到该特定线程进行处理。

Have you tried to run this in debug mode in eclipse? It usually shows all the threads allocated as well as their statuses.
I suspect that in your case the following scenario takes place: you have thread pool and thread pool has allocated number of threads to process the tasks you are submitting into it. But once any particular thread in the thread pool has completed the task submitted into the thread pool it does not terminate. Instead it switches its status to BLOCKED or WAITING (I don't remember for sure which one of them) and waits until the next task is passed into this particular thread for processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文