I have an Executor
that needs to terminate before I can shut down another Executor
, I was thinking about trying to implement a wait-notify tactic, but the notification will have to come from executor.isTerminated()
, so unless I subclass it, I can't notify the shutdown thread. Is there an alternative to sub-classing it, or spin waiting?
Asked
Active
Viewed 66 times
0

Sotirios Delimanolis
- 274,122
- 60
- 696
- 724

Sarah Szabo
- 10,345
- 9
- 37
- 60
-
Listener/observer pattern? – Hovercraft Full Of Eels Jan 13 '15 at 01:48
-
1@HovercraftFullOfEels What would the subject be? If it's the executor, it still sounds like I'd have to subclass it. – Sarah Szabo Jan 13 '15 at 01:50
-
Why do you need to shut down executors in order? – user253751 Jan 13 '15 at 01:51
-
@immibis The first executor works computations, the other one stores the results to the disk. If it's shut down in the wrong order the results of the computations will be lost. – Sarah Szabo Jan 13 '15 at 01:55
-
1Maybe I'm misinterpreting your question, but why not use a CompletionService to wrap your ExecutorService? [for example](http://stackoverflow.com/q/25231149/522444) – Hovercraft Full Of Eels Jan 13 '15 at 01:57
-
I think I'm still not entirely sure why you're trying to do this. Can you post some code to demonstrate your problem? – user253751 Jan 13 '15 at 02:09
2 Answers
0
Executor executor = Executors.newFixedThreadPool(YOUR_THREAD_COUNT);
exector.shutDown();
boolean shutdown = false;
try {
shutdown = executor.awaitTermination(YOUR_TIME_OUT, TimeUnit.MILLISECOND);
} catch (InterruptedException e) {
// handle the exception your way
}
if (!shutdown) {
LOG.error("Executor not shut down before time out.");
}

Alex Suo
- 2,977
- 1
- 14
- 22
0
I think maybe you want a synchronous termination of the executor. Always we can use a workaround to do this:
//THE LOGIC BEFORE YOU WANT TO WAIT THE EXECUTOR
...
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
//THE LOGIC AFTER THE EXECUTOR TERMINATION
...
Using the awaitTermination method with a MAX_VALUE time.

vmcloud
- 650
- 4
- 13