7 thread pool parameters

In Java, we generally achieved by integrating the Thread class and Runnnable interfaces, call the start of the thread () method to start the thread. But many if the number of concurrent, and each thread is executed very short period of time will be the end, so frequent thread creation and destruction process will greatly reduce the efficiency of system operation. The thread pool is to solve the low efficiency of multi-threading problems that arise, he makes the thread can be reused, after the end of the thread is do not be destroyed, but can continue to perform other tasks. (Where you can use the example tomcat do think)

java.uitl.concurrent.ThreadPoolExecutor thread pool class is a core class, four construction method in ThreadPoolExecutor. Source code can be found in front of the last three constructors are called fourth constructor to initialize.

public class ThreadPoolExecutor extends AbstractExecutorService {
    .....
    public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
            BlockingQueue<Runnable> workQueue);
 
    public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
            BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory);
 
    public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
            BlockingQueue<Runnable> workQueue,RejectedExecutionHandler handler);
 
    public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
        BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler);
    ...
}
参数理解:

corePollSize: a core number of threads. After you create a thread pool thread does not have any thread to wait until the arrival of a mission to create a thread to perform the task. By default, after you create a thread pool, the number of threads in the thread pool is 0, when there are tasks to later, it will create a thread to perform the task, when the number of threads in the thread pool reaches corePoolSize, will reach the the task queue them into the cache.

maximumPoolSize: maximum number of threads. It shows that the number of threads in the thread up to be able to create.

keepAliveTime: idle time threads reserved.

TimeUnit: Retention time in idle thread.

TimeUnit.DAYS; // day
TimeUnit.HOURS; // hours
TimeUnit.MINUTES; // min
TimeUnit.SECONDS; // second
TimeUnit.MILLISECONDS; // ms
TimeUnit.MICROSECONDS; // delicate
TimeUnit.NANOSECONDS; // ns
BlockingQueue <Runnable>: blocking queue storage tasks awaiting execution. Parameters ArrayBlockingQueue, LinkedBlockingQueue, SynchronousQueue optional.

ThreadFactory: thread factory used to create a thread

RejectedExecutionHandler: queue is full, and the task is greater than the exception-handling strategy largest thread. The following values

ThreadPoolExecutor.AbortPolicy: RejectedExecutionException discard task and throw an exception. 
ThreadPoolExecutor.DiscardPolicy: the task is discarded, but does not throw an exception. 
ThreadPoolExecutor.DiscardOldestPolicy: discard the foremost task queue, and then try to perform the task (Repeat this process) re
ThreadPoolExecutor.CallerRunsPolicy: handle the task by the calling thread

description link: https: //blog.csdn.net/hsf15768615284/article/details/ 78,261,173

Published 740 original articles · won praise 65 · Views 100,000 +

Guess you like

Origin blog.csdn.net/qq_41723615/article/details/104365060