heduledThreadPoolExecutors和自定义队列

heduledThreadPoolExecutors和自定义队列

本文介绍了ScheduledThreadPoolExecutors和自定义队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I use a ThreadPoolExecutor I have a variety of constructors and I can pass/use my own queue for the pool's work queue.
Now I see that a ScheduledThreadPoolExecutor is a subclass of ThreadPoolExecutor but the constructors are much less.
Is there a way to use a ScheduledThreadPoolExecutor and still use my own work queue?

推荐答案

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

You can extend ScheduledThreadPoolExecutor class and use a different queue then the DelayedWorkQueue that is bound to the current ScheduledThreadPoolExecutor implementation. Note that DelayedWorkQueue is only a BlockingQueue implementation that is using a DelayQueue behind the scene.

但是,如果您只需要配置min,max,keepAlive或其他参数(不需要更改DelayedWorkQueue),则只会扩展ThreadPoolExecutor(类似于ScheduledThreadPoolExecutor所做的事情),并且在您的构造函数,您将执行与ScheduledThreadPoolExecutor构造函数当前正在执行的操作类似的操作,将委派给ThreadPoolExecutor,例如:

But if you only need to configure min, max, keepAlive or other parameters (don't need to change the DelayedWorkQueue) you will only extend ThreadPoolExecutor (similar to what ScheduledThreadPoolExecutor is doing) and in your constructor you will do something similar to what is ScheduledThreadPoolExecutor constructors is doing right now, delegate to the ThreadPoolExecutor like:

super(min, max, keepAliveTime, TimeUnit.NANOSECONDS,
   new CustomQueue(), threadFactory);

这篇关于ScheduledThreadPoolExecutors和自定义队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 01:11