如果我使用ThreadPoolExecutor
,我有多种构造函数,我可以为池的工作队列传递/使用我自己的队列。
现在我看到 ScheduledThreadPoolExecutor
是 ThreadPoolExecutor
的子类,但构造函数要少得多。
有没有办法在使用 ScheduledThreadPoolExecutor
的同时仍然使用我自己的工作队列?
最佳答案
您可以扩展 ScheduledThreadPoolExecutor
类并使用与绑定(bind)到当前 ScheduledThreadPoolExecutor
实现的 DelayedWorkQueue
不同的队列。请注意,DelayedWorkQueue
只是在后台使用 DelayQueue
的 BlockingQueue
实现。
但是如果你只需要配置min,max,keepAlive或其他参数(不需要改变DelayedWorkQueue
)你将只扩展ThreadPoolExecutor
(类似于ScheduledThreadPoolExecutor
正在做什么)并且在你的构造函数中你将做一些类似于 ScheduledThreadPoolExecutor
构造函数现在正在做的事情,委托(delegate)给 ThreadPoolExecutor
就像:
super(min, max, keepAliveTime, TimeUnit.NANOSECONDS,
new CustomQueue(), threadFactory);
关于java - ScheduledThreadPoolExecutors 和自定义队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13514441/