为什么没有Java Executors类提供的计划的缓存线程池? [英] Why is there no scheduled cached thread pool provided by the Java Executors class?
问题描述
执行者
提供 newCachedThreadPool()
和 newScheduledThreadPool()
,但不是 newCachedScheduledThreadPool()
,这里有什么?我有一个应用程序接收突发消息,并需要在每个固定延迟后安排相当长的处理步骤。时间约束不是超紧,但我宁愿在运行时创建更多的线程,如果我超过池大小,然后在不活动期间修剪它们。在并发库中有没有我错过的,或者我需要自己编写?
Executors
provides newCachedThreadPool()
and newScheduledThreadPool()
, but not newCachedScheduledThreadPool()
, what gives here? I have an application that receives bursty messages and needs to schedule a fairly lengthy processing step after a fixed delay for each. The time constraints aren't super tight, but I would prefer to have more threads created on the fly if I exceed the pool size and then have them trimmed back during periods of inactivity. Is there something I've missed in the concurrent library, or do I need to write my own?
推荐答案
通过设计ScheduledThreadPoolExecutor是固定大小。您可以使用提交到正常ExecutorService的单个线程版本来执行任务。这个事件线程+工作者池是相当容易协调和灵活性弥补专用线程。我以前用这个替换TimerTasks和其他非关键任务,以使用一个共同的执行器作为一个系统范围的池。
By design the ScheduledThreadPoolExecutor is a fixed size. You can use a single threaded version that submits to a normal ExecutorService for performing the task. This event thread + worker pool is fairly ease to coordinate and the flexibility makes up for the dedicated thread. I've used this in the past to replace TimerTasks and other non-critical tasks to utilize a common executor as a system-wide pool.
这篇关于为什么没有Java Executors类提供的计划的缓存线程池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!