...
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<>
)
This compliant solution refactors the task so that it catches Throwable
and forwards it to a custom exception reporter (see EXC01-J. Use a class dedicated to reporting exceptions for details on class MyExceptionReporter
).uses a Future<>
object to catch any exception thrown by the task.
Code Block | ||
---|---|---|
| ||
// ... final class Task implements Runnable { @Overridefinal class PoolService { private final ExecutorService pool = Executors.newFixedThreadPool(10); public void rundoSomething() { try { Future<?> future = pool.submit(new Task()); // ... do other work throw new NullPointerException();try { // ...future.get(); } catch (ThrowableInterruptedException te) { // Execute... anyhandle recovery codeinterrupt } catch MyExceptionReporter.report(t); (ExecutionException e) { } finally { Throwable exception = e.getCause(); // exception Performindicates clean-upwhat actionshappened } } } } |
...
Compliant Solution (ThreadPoolExecutor
hooks)
...