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

ThreadPoolExecutor doesn't count throwing tasks as "completed"

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 7
    • 6
    • core-libs
    • b08
    • generic
    • generic
    • Not verified

      If a task throws, completedTasks is not incremented, which means
      both getCompletedTaskCount and getTaskCount are messed up.

      The documentation of afterExecute strongly implies that tasks that
      throw have "completed", since it distingishes those that have
      "completed normally".

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

      public class Bug3 {
          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(); }};

          public static void main(String[] args) throws Throwable {
      Thread.setDefaultUncaughtExceptionHandler(handler);
      ThreadPoolExecutor tpe =
      new ThreadPoolExecutor(3, 3, 0L, TimeUnit.SECONDS,
      new LinkedBlockingQueue<Runnable>());

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

      tpe.shutdown();
      tpe.awaitTermination(100L, TimeUnit.HOURS);
      System.out.printf("count=%d%n", count.get());
      System.out.printf("submitted=%d%n", tpe.getTaskCount());
      System.out.printf("completed=%d%n", tpe.getCompletedTaskCount());
          }
      }
      ----


      ==>

      count=100
      submitted=0
      completed=0

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: