[ACCEPTED]-ScheduledExecutorService start stop several times-scheduler
Accepted answer
You can reuse the scheduler, but you shouldn't 3 shutdown it. Rather, cancel the running 2 thread which you can get when invoking scheduleAtFixedRate 1 method. Ex:
//get reference to the future
Future<?> future = service.scheduleAtFixedRate(runnable, INITIAL_DELAY, INTERVAL, TimeUnit.SECONDS)
//cancel instead of shutdown
future.cancel(true);
//schedule again (reuse)
future = service.scheduleAtFixedRate(runnable, INITIAL_DELAY, INTERVAL, TimeUnit.SECONDS)
//shutdown when you don't need to reuse the service anymore
service.shutdown()
The javadocs of shutdown()
say:
Initiates an orderly shutdown in which previously submitted tasks are executed,
but no new tasks will be accepted.
So, you cannot call 1 shutdow()
and then schedule new tasks.
You can't make your executor accept new 5 tasks after shutting it down. The more 4 relevant question is why you need to shut 3 it down in the first place? The executors 2 you create should be re-used across the 1 lifetime of your application or sub-system.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.