Public Threadpoolexecutor (int corepoolsize, int maximumpoolsize, Long KeepAliveTime, timeunit unit, blockingqueue<Runnable> workQueue)
The following explains the meanings of each parameter in the constructor:
- Corepoolsize: The size of the core pool, this parameter is very much related to the implementation principle of the thread pool described later. After the thread pool has been created, by default, there are no threads in the thread pools, instead of waiting for a task to be created to perform the task, unless the prestartallcorethreads () or Prestartcorethread () method is called. As you can see from the names of these 2 methods, the meaning of the pre-created thread is to create a corepoolsize thread or a thread before the task arrives. By default, after a thread pool has been created, the number of threads in the thread pools is 0, and when a task comes, a thread is created to perform the task, and when the number of threads in the thread pool reaches corepoolsize, the incoming task is placed in the cache queue;
- Maximumpoolsize: Thread pool Maximum number of threads, this parameter is also a very important parameter, it represents the maximum number of threads in the thread pool can be created;
- KeepAliveTime: Indicates the maximum length of time a thread will be terminated without a task executing. By default, KeepAliveTime only works if the number of threads in the thread pool is greater than corepoolsize, until the number of threads in the thread pool is not greater than corepoolsize, that is, when the number of threads in the thread pool is greater than corepoolsize. If a thread is idle for KeepAliveTime, it terminates until the number of threads in the thread pool does not exceed corepoolsize. However, if the Allowcorethreadtimeout (Boolean) method is called, the KeepAliveTime parameter also works when the number of threads in the thread pool is not greater than corepoolsize, until the number of threads in the thread pool is 0;
- Unit: The time unit of the parameter KeepAliveTime, there are 7 kinds of values, there are 7 kinds of static properties in the Timeunit class:
Timeunit.days; Days Timeunit.hours; Hour timeunit.minutes; Minutes Timeunit.seconds; Seconds timeunit.milliseconds; Ms Timeunit.microseconds; Subtle timeunit.nanoseconds; Na-Sec
- WorkQueue: A blocking queue that stores the tasks waiting to be executed, and the choice of this parameter is also important to have a significant impact on the running process of the thread pool, in general, where the blocking queue has the following options:
Arrayblockingqueue; Linkedblockingqueue; Synchronousqueue;
Arrayblockingqueue and Priorityblockingqueue use less, generally using linkedblockingqueue and synchronous. The thread pool's queuing policy is related to Blockingqueue.
- Threadfactory: Thread factory, mainly used to create threads;
- Handler: Represents the following four types of values when a policy is rejected when processing a task:
New New Arrayblockingqueue<runnable> (5)); for (int i=0; i<; i++) { executor.execute (new Robot ()); } Executor.shutdown ();
Java Thread pool Usage