ScheduledThreadPoolExecutors 和自定义队列

Jim*_*Jim 5 java concurrency multithreading executorservice executors

如果我使用 a,ThreadPoolExecutor我有多种构造函数,并且我可以传递/使用我自己的队列作为池的工作队列。
现在我看到 aScheduledThreadPoolExecutor是 a 的子类ThreadPoolExecutor,但构造函数要少得多。
有没有办法使用ScheduledThreadPoolExecutor并且仍然使用我自己的工作队列?

dan*_*dan -3

您可以扩展类并使用与绑定到当前实现的ScheduledThreadPoolExecutor队列不同的队列。请注意,这只是在幕后使用的实现。DelayedWorkQueueScheduledThreadPoolExecutorDelayedWorkQueueBlockingQueueDelayQueue

但是,如果您只需要配置 min、max、keepAlive 或其他参数(不需要更改),DelayedWorkQueue您将只扩展ThreadPoolExecutor(类似于ScheduledThreadPoolExecutor正在做的事情),并且在构造函数中您将执行类似于ScheduledThreadPoolExecutor构造函数正在做的事情现在,委托给ThreadPoolExecutor类似的人:

super(min, max, keepAliveTime, TimeUnit.NANOSECONDS,
   new CustomQueue(), threadFactory);
Run Code Online (Sandbox Code Playgroud)