Why is there no scheduled cached thread pool provided by the Java Executors class?

前端 未结 4 477
庸人自扰
庸人自扰 2020-12-30 19:20

Executors provides newCachedThreadPool() and newScheduledThreadPool(), but not newCachedScheduledThreadPool(), what gives

相关标签:
4条回答
  • 2020-12-30 19:51

    Like skaffman says, Executors is only a collection of factory method. if you need a particular instance, you can always check all existing Executor implementors. In your case, i think that calling one of the various constructors of ScheduledThreadPoolExecutor would be a good idea.

    0 讨论(0)
  • 2020-12-30 19:57

    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.

    0 讨论(0)
  • 2020-12-30 20:01

    Suggested here Why does ScheduledThreadPoolExecutor only accept a fixed number of threads? workaround:

    scheduledExecutor = new ScheduledThreadPoolExecutor(128); //no more than 128 threads
    scheduledExecutor.setKeepAliveTime(10, TimeUnit.SECONDS);
    scheduledExecutor.allowCoreThreadTimeOut(true);
    
    0 讨论(0)
  • 2020-12-30 20:04

    java.util.concurrent.Executors is nothing more than a collection of static convenience methods that construct common arrangements of executors.

    If you want something specific that isn't offered by Executors, then feel free to construct your own instance of the implemention classes, using the examples in Executors as a guide.

    0 讨论(0)
提交回复
热议问题