英文:
What does "active threads" mean in ThreadPoolExecutor?
问题
我有一个ThreadPoolExecutor,其参数如下:
corePoolSize = 20
maximumPoolSize = 100
workQueue = new LinkedBlockingQueue(10)
每秒我提交1,000个任务。这些任务代表一些需要1-2秒的IO操作。其中一些被拒绝了,这是预期的。
我在拒绝之前使用自定义的AbortPolicy记录线程池状态。
在日志中,我看到了一些我无法解释的信息。类似这样的消息:
java.util.concurrent.ThreadPoolExecutor@3e6be2d8[Running, pool size = 100, active threads = 2, queued tasks = 10, completed tasks = 4471827].
我在文档中找到了getActiveCount
方法,它返回正在执行任务的近似线程数
。但这并没有帮助我理解...
这里的"active threads"是什么意思?为什么在池已达到其限制、队列已满且任务被拒绝的情况下只有2个线程是活动的?
我想增加maximumPoolSize
以一次处理更多任务,但首先我需要了解是否有效地使用了ThreadPool的资源。
英文:
I have ThreadPoolExecutor whith next params:
corePoolSize = 20
maximumPoolSize = 100
workQueue = new LinkedBlockingQueue(10)
Each second I submit 1k tasks. Tasks represent some IO operation which takes 1-2s. Some of them get rejected. It's expected.
I log the thread pool state before rejection using custom AbortPolicy.
In logs I see something I cannot explain. Messages like:
> java.util.concurrent.ThreadPoolExecutor@3e6be2d8[Running, pool size = 100, active threads = 2, queued tasks = 10, completed tasks = 4471827].
I've found in docs that getActiveCount
method Returns the approximate number of threads that are actively executing tasks.
But it does not help me to understand...
What does "active threads" mean here? And why only 2 threads are active while pool has reached its limit, queue is full and the task is rejected?
I want to increase maximumPoolSize
to process more tasks at once but firstly I need to understand whether I use ThreadPool resources efficiently.
答案1
得分: 2
"Active thread" 意味着已经开始执行工作队列中的任务的线程。
当线程启动或变为空闲时,它们不会立即“立刻”选择任务 - 从队列中读取需要争夺。如果您在一个紧密的循环中提交了100个任务给执行器,最后只有少数几个工作线程有机会选择任务。
为了更好地了解实际正在运行任务的线程数量,在定期的时间表上记录活动线程计数,例如每秒10次。
英文:
"Active thread" means a thread that has started executing a task from the work queue.
When threads are started or become free, they don't pick up a task "immediately" - there's contention to read from the queue. If you submit 100 tasks to the executor in a tight loop, it's likely that by the end only a few worker threads have had the chance to pick up a task.
To get a better picture of how many threads are actually running tasks, log the active thread count on a regular schedule, for example 10 times per second.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论