ScheduledThreadPoolExecutors and custom queue

痴心易碎 提交于 2019-12-10 15:20:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!