Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6450205

ThreadPoolExecutor does not replace throwing threads

XMLWordPrintable

    • b08
    • generic
    • generic
    • Not verified

      Neal wrote:

      >> Executor seems to take roughly a minute to replace threads that have
      >> died, rather than replacing them immediately.

      --------------
      import java.util.*;
      import java.util.concurrent.*;
      import java.util.concurrent.atomic.*;

      public class Bug5 {
          private static final AtomicInteger count
      = new AtomicInteger(0);

          private static final Thread.UncaughtExceptionHandler handler
      = new Thread.UncaughtExceptionHandler() {
      public void uncaughtException(Thread t, Throwable e) {
      count.getAndIncrement(); }};

          static void report(String label, ThreadPoolExecutor tpe) {
      System.out.printf("%10s: active=%d, submitted=%d, completed=%d%n",
      label,
      Thread.activeCount() - 1,
      tpe.getTaskCount(),
      tpe.getCompletedTaskCount());
          }

          public static void main(String[] args) throws Throwable {
      Thread.setDefaultUncaughtExceptionHandler(handler);

      final int count = 8;
      final CyclicBarrier barrier = new CyclicBarrier(count + 1);
      ThreadPoolExecutor tpe =
      new ThreadPoolExecutor(10, 30, 1L, TimeUnit.HOURS,
      new LinkedBlockingQueue<Runnable>());

      report("newborn", tpe);

      for (int i = 0; i < count; i++)
      tpe.execute(new Runnable() {
      public void run() {
      try { barrier.await(); barrier.await(); }
      catch (Throwable t) { t.printStackTrace(); }
      }});

      barrier.await();
      report("started", tpe);

      barrier.await();
      Thread.sleep(1000);
      report("idling", tpe);

      for (int i = 0; i < count; i++)
      tpe.execute(new Runnable() {
      public void run() {
      throw new RuntimeException();
      }});

      Thread.sleep(1000);

      report("thrown", tpe);

      System.out.printf("Shutting down...%n");
      tpe.shutdown();
      tpe.awaitTermination(1L, TimeUnit.HOURS);

      report("terminated", tpe);
          }
      }
      --------------

       $ jver 6 jr Bug5
      ==> javac -source 1.6 -Xlint:all Bug5.java
      ==> java -esa -ea Bug5
         newborn: active=0, submitted=0, completed=0
         started: active=8, submitted=8, completed=0
          idling: active=8, submitted=8, completed=8
          thrown: active=2, submitted=8, completed=8
      Shutting down...
      terminated: active=0, submitted=8, completed=8

      The active count should not go down to 2.

            martin Martin Buchholz
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: