0

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?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Sarah Szabo
  • 10,345
  • 9
  • 37
  • 60

2 Answers2

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