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?
Asked
Active
Viewed 2,112 times
5

Jim
- 18,826
- 34
- 135
- 254
-
1`ScheduledThreadPoolExecutor`'s implementation seems to be dependent on the use of a custom `DelayedWorkQueue`. The class would probably not work any longer with a different queue. – assylias Nov 22 '12 at 14:09
-
What about the other configuration? Min-Max pool size,keepAlive time etc? – Jim Nov 22 '12 at 14:16
1 Answers
-3
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);

dan
- 13,132
- 3
- 38
- 49
-
In your example `super(...)` will call ScheduledThreadPoolExecutor's constructor. – assylias Nov 22 '12 at 14:34
-
@assylias Thanks, you are correct, I forgot to mention that if he needs to alter the parameters too, he will need to extend `ThreadPoolExecutor`, updated my answer. – dan Nov 22 '12 at 14:38
-
-
@Jim Then, like I said in my answer, you will extend `ThreadPoolExecutor` and use a `CustomQueue`, similar to what `ScheduledThreadPoolExecutor` is doing. – dan Nov 22 '12 at 14:52
-
-
@Jim If you will extend `ThreadPoolExecutor` you will be able to schedule tasks, that is what `ScheduledThreadPoolExecutor` is doing also, see the source. Basically what you need is to create a class that will be similar to `ScheduledThreadPoolExecutor` but will have different constructors and a different queue. – dan Nov 22 '12 at 15:01
-
Wait, does this solution imply re-implementing all the logic in `ScheduledThreadPoolExecutor`? – voddan May 29 '18 at 15:51
-
@voddan No, in this case you were only overriding (re-implementing) the constructor. When extending you can add new functionality by adding new methods or override existing methods (in this case the constructor) to change their base behavior. – dan May 29 '18 at 17:41
-
@dan it says here a constructor cannot be overriden in java: https://stackoverflow.com/questions/5099924/is-constructor-overriding-possible – voddan May 29 '18 at 20:35
-
@voddan In my comment I was referring to the general case for methods, for you to understand that you don't need to re-implement everything, all the methods (logic as you said). The constructor is behaving like a method, but it is different, and in this case we don't say that it is overriding. In this case the OP had only to define the constructor to use his own queue. Not sure why you down voted, the answer was not for your comment, can you explain? – dan May 30 '18 at 06:50
-
Your answer does not solve the question because AFAIK it is impossible to implement, precisely because there is NO constructor for `ScheduledThreadPoolExecutor` that allows custom queues. Not sure why it was accepted in the first place TBF – voddan May 30 '18 at 07:48
-
@voddan Every class has a constructor, java is generating one, if you don't create one. But, if you read carefully the answer you will find that it mentions to extend `ThreadPoolExecutor` to be able to solve what OP requested. Anyway, hope it is more clear to you now, and "thanks" for the down vote. – dan May 30 '18 at 08:27
-
1@dan I don't think that extending `ThreadPoolExecutor` and re-implementing `ScheduledThreadPoolExecutor`'s whole logic is an answer anyone would go to SO for. You are welcome. – voddan May 30 '18 at 09:29
-
Re-implementing ScheduledThreadpoolExcecutor is not the expected solution like @dan said. Hence down voted. – Pragalathan M May 21 '19 at 20:31