ScheduledThreadPoolExecutors 和自定义队列

2023-12-20

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


你可以延长ScheduledThreadPoolExecutor类并使用不同的队列然后DelayedWorkQueue与当前绑定的ScheduledThreadPoolExecutor执行。注意DelayedWorkQueue只是一个BlockingQueue使用的是一个实现DelayQueue在幕后。

但如果你只需要配置min、max、keepAlive或者其他参数(不需要改变DelayedWorkQueue)你只会延长ThreadPoolExecutor(类似于什么ScheduledThreadPoolExecutor正在做)并且在你的构造函数中你将做类似的事情ScheduledThreadPoolExecutor构造函数现在正在做,委托给ThreadPoolExecutor like:

super(min, max, keepAliveTime, TimeUnit.NANOSECONDS,
   new CustomQueue(), threadFactory);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ScheduledThreadPoolExecutors 和自定义队列 的相关文章

随机推荐