Reputation: 31
The typical ExecutorService example shows using a while loop that simple tests isTerminated after a shutdown request. My thinking is that the while loop is being called too often to do not much of anything.
Would it be okay to place a Thread.sleep(x) within the loop so it frees up some processor resources?
es.shutdown();
while (!es.isTerminated()) {
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
}
Upvotes: 2
Views: 130
Reputation: 140494
The main things wrong here are:
InterruptedException
to propagate, but alternatively by re-interrupting the thread.It would be better to use
es.awaitTermination(60, SECONDS);
in order to stop as soon as the executor terminates.
And, when you catch the interruption:
Thread.currentThread().interrupt();
to allow it to propagate.
Or, forgo the loop and just sleep for a really long time:
es.awaitTermination(Long.MAX_VALUE, SECONDS);
Upvotes: 3
Reputation: 1817
Within this specific scenario, using the Thread.sleep
would only make sense if you absolutely need the executor service to be dead before you perform any additional logic.
If this is not the case then pausing the main thread with sleep while the executor is shutting down does not make much sense, the Jvm will not free many resources while doing that Thread.sleep(60000)
Upvotes: 0