I use ScheduledExecutorService in my application. I need to use it from time to time in certain Utility class to run scheduled threads.
Is it a good design to hold ScheduledExecutorService in static field? Is it a must to invoke ScheduledExecutorService.shutdown() in such case? What is the risk if I do not invoke shutdown?
That's what I thought to do:
private static ScheduledExecutorService exec = Executors.newScheduledThreadPool(5);
public void scheduleTask(String name) {
Future<?> future = futuresMapping.get(name);
if(future!=null && !future.isDone())
future.cancel(true);
//execute once
Future<?> f = scheduledExecutor.schedule(new MyTask()), 1, TimeUnit.MINUTES);
futuresMapping.put(name, f);
}
Thank you