An executor
will throw RejectedExecutionException if it has been shut down when you try to submit a new job for it to run. Your executor
variable is declared static
, which means all instances of the class will share the same variable. Is it possible that one instance of your class is shutting down the executor, and then another instance of the class is trying to schedule a new job?
ExecutorService throws RejectedExecutionException if only initialized with declaration
-
28-06-2022 - |
Question
i got some code and if I only use the declaration with initialization as in
private static ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
this will throw me a java.util.concurrent.RejectedExecutionException
on
executor.scheduleWithFixedDelay(runnable, 0, 2000, TimeUnit.MILLISECONDS);
but if I initialize again before as with
executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleWithFixedDelay(runnable, 0, 2000, TimeUnit.MILLISECONDS);
everything runs normal. Why is that? All examples I found do not execute the SingleThreadExecutor every time before scheduling. I thought it's just to set the pool size. And it should be working being intialized during declaration?! I'm confused :) Thanks
Solution
OTHER TIPS
Two things.
- Avoid declaring anything static that isn't final.
- It was probably
shutdown
at some point.
nevermind, it was a shutdown -.- i expected isTerminated to be true when no task was yet run -.- thanks for wasting your time for me :p
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow