Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: excessive "new" removed.

...

Code Block
bgColor#ccccff
final class PoolService {
  private static final ThreadFactory factory = new
      new ExceptionThreadFactory(new MyExceptionHandler());
  private static final ExecutorService pool =
      Executors.newFixedThreadPool(10, factory);

  public void doSomething() {
    pool.execute(new Task()); // Task is a runnable class
  }

  public static class ExceptionThreadFactory implements ThreadFactory  {
    private static final ThreadFactory defaultFactory =
        Executors.defaultThreadFactory();
    private final Thread.UncaughtExceptionHandler handler;

    public ExceptionThreadFactory(
        Thread.UncaughtExceptionHandler handler)
    {
      this.handler = handler;
    }

    @Override public Thread newThread(Runnable run) {
      Thread thread = defaultFactory.newThread(run);
      thread.setUncaughtExceptionHandler(handler);
      return thread;
    }
  }

  public static class MyExceptionHandler extends ExceptionReporter
      implements Thread.UncaughtExceptionHandler {
    // ...

    @Override public void uncaughtException(Thread thread, Throwable t) {
      // Recovery or logging code
    }
  }
}

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b07498b32a8965d1-f840fb30-47414f41-abb8883c-e5f5de58a2c41be262b63230"><ac:plain-text-body><![CDATA[

[[API 2006

AA. References#API 06]]

Interfaces ExecutorService, ThreadFactory; class Thread

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="81363463481ffb87-80308a0e-43734dec-ba3e8c37-d3937c9f08709ff4785d4acc"><ac:plain-text-body><![CDATA[

[[Goetz 2006

AA. References#Goetz 06]]

Chapter 7.3, Handling Abnormal Thread Termination

]]></ac:plain-text-body></ac:structured-macro>

...