Creation of a thread pool
ThreadFactory USES the default Executors. DefaultThreadFactory ()
ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler)
Copy the code
CorePoolSize:
The number of core threads, even if idle, that remain in the pool unless allowCoreThreadTimeOut is set to true. When a task is submitted to the thread pool, the thread pool creates a new thread to execute the task, even if other idle threads are able to execute the new task. When the number of tasks that need to be executed is greater than corePoolSize, no more threads are created. New tasks are added to the BlockingQueue.
MaximumPoolSize:
The maximum number of threads that a thread pool can hold. If the queue is full and the number of created threads is less than maximumPoolSize, the thread pool creates a new thread to execute the task. If an unbounded task queue is used, the maximumPoolSize parameter has no effect.
KeepAliveTime:
The number of worker threads in a thread pool that remain alive after they become idle. If there are many tasks and the execution time is short, you can adjust the execution time to improve thread utilization.
WorkQueue:
Task queues. A blocking queue used to hold tasks waiting to be executed. Commonly used are:
ArrayBlockingQueue: a bounded blocking queue based on an array structure that sorts elements in FIFO.
(2)LinkedBlockingQueue: a bounded blocking queue based on a linked list structure that sorts elements in FIFO. Static methods Executors. NewFixedThreadPool () and Executors. NewSingleThreadExecutor () using the queue. The default length and maximum length of this queue are integer.max_value
SynchronousQueue: a blocking queue that does not store elements. Each insert operation must wait until another thread calls the remove operation, otherwise the insert operation remains blocked. Static methods Executors. NewCachedThreadPool () using the queue.
(4)PriorityBlockingQueue: an unbounded blocking queue with priority.
RejectedExecutionHandler:
Reject policy. If the number of submitted tasks exceeds the sum of maxmumPoolSize+workQueue, the task will be processed by RejectedExecutionHandler. The Java thread pool framework provides four policies: four rejection policies