...
The task does not notify upper layers when it terminates unexpectedly as a result of the runtime exception. Moreover, it does not use any recovery mechanism.
Compliant Solution (
...
Future<V>
)
This compliant solution uses a Future<>
Future
object to catch any exception thrown by the task. It uses ExecutorService.submit()
method to submit the task so that a Future
can be obtained.
Code Block | ||
---|---|---|
| ||
final class PoolService { private final ExecutorService pool = Executors.newFixedThreadPool(10); public void doSomething() { Future<?> future = pool.submit(new Task()); // ... do other work try { future.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // ... handle interrupt Reset interrupted status } catch (ExecutionException e) { Throwable exception = e.getCause(); // exceptionForward indicatesto whatexception happenedreporter } } } |
Compliant Solution (ThreadPoolExecutor
hooks)
...