问题
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?
回答1:
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.
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);
来源:https://stackoverflow.com/questions/13514441/scheduledthreadpoolexecutors-and-custom-queue