Java thread pool constructor arguments detailed

There are four class constructor ThreadPoolExecutor final call is a function of:

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler)

Constructor a total of seven parameters as follows:

corePoolSize

The core number of threads in the thread pool, when submitting a job, create a new thread pool thread to perform the task until the current number of threads equal corePoolSize; if the current number of threads corePoolSize, continue to submit tasks are saved to the blocking queue, waiting to be execution; if prestartAllCoreThreads thread pool implemented () method, the thread pool will create and start all core threads in advance. When the number of threads less corePoolSize, by default the thread in the thread pool will always survive, instant thread is free. If allowCoreThreadTimeOut is set to true, no matter how much the number of threads, the thread is idle over a certain time it will be destroyed off.

maximumPoolSize

The maximum number of threads in the thread pool allowed. If the current blocking queue is full, and continue to submit tasks, create a new thread to perform the task, provided that the current number of threads less than maximumPoolSize;

keepAliveTime

Survival time when the thread is idle, that is, when the thread is not the mandate, to survive the time; by default, this parameter is only useful when the number of threads is greater than corePoolSize; if allowCoreThreadTimeOut is set to true, no matter how much the number of threads, thread idle more than a certain time it will be destroyed off.

unit

keepAliveTime units. TimeUnit is an enumerated type, including:

  • NANOSECONDS: 1 msec = 1 sec micro / 1000
  • MICROSECONDS: 1 microsecond = 1 ms / 1000
  • MILLISECONDS: 1 msec = 1 sec / 1000
  • SECONDS: seconds
  • MINUTES :分
  • HOURS: hours
  • DAYS: Days

workQueue

To save the task waiting to be executed blocking queue, and the task must implement the interface Runable, blocking queue as follows:

  • ArrayBlockingQueue: array-based structure bounded blocking queue, sorting task by FIFO;
  • LinkedBlockingQuene: unbounded blocking queue based on the list structure, a FIFO sorting task, usually higher than a certain ArrayBlockingQuene;
  • SynchronousQuene: a storage element without blocking queue, each insert operation must wait until another thread calls the removal operation, or insert operation has been in a blocked state, throughput is usually higher than LinkedBlockingQuene;

threadFactory

Create a thread factory by a thread factory can customize settings for each new thread has a thread name recognition level, such as:

public class OneMoreThreadFactory implements ThreadFactory {
    private static final AtomicInteger poolNumber = new AtomicInteger(1);
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final String namePrefix;

    public OneMoreThreadFactory() {
        namePrefix = "OneMoreThread-" + poolNumber.getAndIncrement() + "-";
    }

    @Override
    public Thread newThread(Runnable r) {
        return new Thread( r, namePrefix + threadNumber.getAndIncrement());
    }
}

handler

Saturated strategies thread pool, when the blocking queue is full, and there is no idle worker threads, if it continues to submit the task, a strategy must be taken to handle the task, the thread pool offers four kinds of strategies:

  • AbortPolicy: direct throw an exception, the default policy;
  • CallerRunsPolicy: with the thread of the caller where to perform the task;
  • DiscardOldestPolicy: discarding the blocking queue by the task before most, and execution of the current task;
  • DiscardPolicy: discards the task;

Guess you like

Origin www.cnblogs.com/heihaozi/p/11734307.html