Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The best way to handle exceptions at the application level is to use an exception handler. The handler can perform diagnostic actions, clean-up and shutdown shut down the JVM, or simply log the details of the failure.

...

Code Block
bgColor#ccccff
final class PoolService {
  // The values have been hardcodedhard-coded for brevity
  ExecutorService pool = new CustomThreadPoolExecutor(10, 10, 10, TimeUnit.SECONDS,
                         new ArrayBlockingQueue<Runnable>(10));
  // ...
}

class CustomThreadPoolExecutor extends ThreadPoolExecutor {
  // ... Constructor ...

  @Override
  public void afterExecute(Runnable r, Throwable t) {
    super.afterExecute(r, t);
    if (t != null) {
      // Exception occurred, forward to handler
    }
    // ... Perform task-specific clean-up actions
  }

  @Override
  public void terminated() {
    super.terminated();
    // ... Perform final clean-up actions
  }
}

...