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

ThreadPoolExecutor idling core threads don't terminate when core pool size reduced

XMLWordPrintable

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

      ThreadPoolExecutor.setCorePoolSize goes to a lot of trouble to
      kill off excess idling threads, but the code is not correct.

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

      public class Bug4 {
          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 {
      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);

      tpe.setCorePoolSize(count/2);
      Thread.sleep(1000);

      report("shrunk", tpe);

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

      report("terminated", tpe);
          }
      }
      --------------
       $ jver 6 jr Bug4
      ==> javac -source 1.6 -Xlint:all Bug4.java
      ==> java -esa -ea Bug4
         newborn: active=0, submitted=0, completed=0
         started: active=8, submitted=8, completed=0
          idling: active=8, submitted=8, completed=8
          shrunk: active=8, submitted=8, completed=8
      Shutting down...
      terminated: active=0, submitted=8, completed=8

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: