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

ThreadPoolExecutor's setCorePoolSize method allows corePoolSize > maxPoolSize

XMLWordPrintable

      The ThreadPoolExecutor constructor throws an IllegalArgumentException if the corePoolSize is greater than maximumPoolSize, but the method called "setCorePoolSize(int corePoolSize)"
      (which overrides values set in the constructor) allows corePoolSize > maxPoolSize. No IllegalArgumentException is thrown in that case. So the constructor checks the values but the setCorePoolSize method does not.

      import java.util.Timer;
      import java.util.concurrent.ArrayBlockingQueue;
      import java.util.concurrent.BlockingQueue;
      import java.util.concurrent.ThreadPoolExecutor;
      import java.util.concurrent.TimeUnit;

      public class Main {

          static void print(ThreadPoolExecutor pool) {
              System.out.printf("poolSize=%d core=%d max=%d%n",
                  pool.getPoolSize(),
                  pool.getCorePoolSize(),
                  pool.getMaximumPoolSize());
          }

          public static void main(String[] args) {
              final int CORE = 2,
                        MAX = 10,
                        KEEP_ALIVE_TIME = 30,
                        QUEUE_MAX_SIZE = 25;
              final BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(QUEUE_MAX_SIZE);
              final ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE, MAX, KEEP_ALIVE_TIME, TimeUnit.SECONDS, workQueue);
              print(executor);
              executor.setCorePoolSize(MAX+1);
              print(executor);
          }
      }

      prints:
      poolSize=0 core=2 max=10
      poolSize=0 core=11 max=10

            prappo Pavel Rappo (Inactive)
            jloefflm Johann Löfflmann (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: