Home > Software engineering >  ScheduledExecutorService with min & max number of threads
ScheduledExecutorService with min & max number of threads

Time:11-18

I am trying to use ScheduledExecutorService to schedule tasks on a given thread pool.

public ScheduledExecutorService scheduledExecutorService() {
    return Executors.newScheduledThreadPool(3);
}

It looks like this way of creating an instance assumes a user-specified value as a minimum number of threads and the maximum number of threads will be Integer.MAX_VALUE (default value).

How can I specify the maximum number of threads for an instance of ScheduledExecutorService? I can't allocate such a huge number of threads as a maximum number of threads.

Code snippets in a sequence from the decompiled jar -

// From Executors.java
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
    return new ScheduledThreadPoolExecutor(corePoolSize);
}

// From ScheduledThreadPoolExecutor.java
public ScheduledThreadPoolExecutor(int corePoolSize) {
    super(corePoolSize, Integer.MAX_VALUE,
          DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
          new DelayedWorkQueue());
}

// From ThreadPoolExecutor.java
public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue) {
    this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
         Executors.defaultThreadFactory(), defaultHandler);
}

If I'm missing to refer a method in Java doc, please point me to the correct location. TIA.

CodePudding user response:

Constructor of ScheduledThreadPoolExecutor does not directly provide the setting of this parameter. You can manually call setMaximumPoolSize method:

    public ScheduledExecutorService scheduledExecutorService() {
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(corePoolSize);
        scheduledThreadPoolExecutor.setMaximumPoolSize(xxx);
        return scheduledThreadPoolExecutor;
    }

But setting this maximumPoolSize is meaningless in ScheduledThreadPoolExecutor , which is why the constructor does not declare this parameter.

From Java doc of ScheduledThreadPoolExecutor :

While this class inherits from ThreadPoolExecutor, a few of the inherited tuning methods are not useful for it. In particular, because it acts as a fixed-sized pool using corePoolSize threads and an unbounded queue, adjustments to maximumPoolSize have no useful effect. Additionally, it is almost never a good idea to set corePoolSize to zero or use allowCoreThreadTimeOut because this may leave the pool without threads to handle tasks once they become eligible to run.

More details, see delayedExecute and ensurePrestart in ScheduledThreadPoolExecutor, maximumPoolSize is useless.

   private void delayedExecute(RunnableScheduledFuture<?> task) {
        if (isShutdown())
            reject(task);
        else {
            // add task to queue
            super.getQueue().add(task);
            if (isShutdown() &&
                !canRunInCurrentRunState(task.isPeriodic()) &&
                remove(task))
                task.cancel(false);
            else
                ensurePrestart();
        }
    }

    void ensurePrestart() {
        int wc = workerCountOf(ctl.get());
        if (wc < corePoolSize)
            addWorker(null, true);
        else if (wc == 0)
            addWorker(null, false);
    }
  • Related