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
An
executor
will throw RejectedExecutionException if it has been shut down when you try to submit a new job for it to run. Yourexecutor
variable is declaredstatic
, 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?