ThreadPool constructor and parameter list

Thread pool constructor and parameter list

During the interview, I asked about the list of constructor parameters of thread pool. Here is a summary:

1, Thread pool type

  • FixedThreadPool
  • CachedThreadPool
  • ScheduledThreadPool

1. FixedThreadPool

FixedThreadPool is to create a thread pool with a fixed number of threads. See the static creation method of Executors:

/**
     * Create a thread pool with a fixed number of threads
     @param nThreads, Initialize thread size
     */
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }



/**
   Initialize fixed size thread pool
   @param nThreads Thread pool size
   @param threadFactory Thread factory
*/
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>(),
                                      threadFactory);
    }

The parameter int nThreads represents the initialization quantity, threadFactory is the creation factory, and ThreadPoolExecutor is returned
The code is as follows:

// Public constructors and methods

    /**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters and default thread factory and rejected execution handler.
     * It may be more convenient to use one of the {@link Executors} factory
     * methods instead of this general purpose constructor.
     *
     * @param corePoolSize Initialize thread pool size
     *        
     * @param maximumPoolSize Maximum threads allowed
     * @param keepAliveTime Maximum active time, when idle more than this time will be closed
     * @param unit Time unit
     * @param workQueue Hold queue waiting for execution thread
     *       
     * @throws IllegalArgumentException This exception is thrown when one of the following occurs
     *         {@code corePoolSize < 0}
     *         {@code keepAliveTime < 0}
     *         {@code maximumPoolSize <= 0}
     *         {@code maximumPoolSize < corePoolSize}
     * @throws NullPointerException if {@code workQueue} is null
     */
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }

CachedThreadPool

First look at the constructor

 public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

 public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>(),
                                      threadFactory);
    }

The maximum number of threads is integer.max'value, a nonparametric construction, a parametric construction, ThreadFactory

SceduledThradPool

Constructor:

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }

public static ScheduledExecutorService newScheduledThreadPool(
            int corePoolSize, ThreadFactory threadFactory) {
        return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
    }

corePoolSize parameter, and thredFactory

Posted on Fri, 20 Mar 2020 14:48:15 -0400 by maya28