1 Introduction to Java thread pools

The work of Java1.5 master Doug Lea. Thread pool can be unified allocation of threads, through a fixed number of threads to handle tasks, avoid frequent creation and destruction of objects, so that threads can be reused, perform multiple tasks.

2 Types of Java thread pools

Thread factory Executors implemented by type

species implementation Blocking queue based instructions
Fixed size thread pool FixedThreadPool LinkedBlockingQueue Fixed number of threads, unbounded task queue
Delivery mode thread pool CachedThreadPool SynchronousQueue The number of threads is unlimited, and the task queue does not store data
Parallel thread pool WorkStealingPool FIFO_QUEUE or LIFO_QUEUE The number of threads is unlimited and the task is divided
Scheduled task thread pool ScheduledThreadPool DelayedWorkQueue There is no limit to the number of threads and tasks are executed according to time priority
Singleton fixed thread pool SingleThreadExecutor LinkedBlockingQueue The number of threads is 1, and the tasks are executed in the order of joining

Implementation of Java thread pool

3.1 Blocking Queue

The queue implementation instructions
LinkedBlockingQueue Queues based on linked lists and display lock implementations Similar to LinkedList supports concurrency
SynchronousQueue Delivery queues based on dual stack/dual queue and display lock implementation Based on double stack/double queue algorithm
DelayedWorkQueue Delay queue based on priority queue and display lock implementation Similar to priority queue, implemented by binary heap algorithm

3.2 Thread Factory Executors

Thread pool body flow

3.2.1 CachedThreadPool

  • Reuse previous threads (threads in the thread pool that have not been destroyed)
  • If no thread is available, a new thread is created and added to the pool
  • Threads in the pool are terminated and removed by default if they are not used for 60 seconds
  • A pool that is idle for a long time will consume no resources

When executing the execute method, the offer method of the SynchronousQueue submits the task first, and the poll method of the SynchronousQueue is queried to see if there are any free threads in the thread pool to execute the poll method to remove the task. If so, the pairing succeeds and the task is handed over to the idle thread. Otherwise, the pairing fails and a new thread is created to process the task. When a thread in the thread pool is idle, the poll method of SynchronousQueue is executed to wait for the execution of newly committed tasks in SynchronousQueue. If no task has been committed to the SynchronousQueue for more than 60 seconds, the idle thread terminates. Since maximumPoolSize is unbounded, the rate at which a task is submitted > the rate at which a thread in the thread pool can process a task constantly creates new threads; Every time a task is submitted, there is an immediate thread to process it, so CachedThreadPool is suitable for handling a large number of small tasks.

Advantages and disadvantages of delivering (cached) thread pools

Advantages: The task is executed immediately and the thread is automatically reclaimed

Disadvantages: The instant too high concurrency situation will appear to create thousands of threads in an instant, causing the system to crash

Usage scenarios

OKHTTP framework

3.2.2 FixedThreadPool

  • Create a thread pool that reuses a fixed number of threads
  • When all threads are active, if other tasks are submitted, they will wait in the queue for a thread to become available
  • When the number of tasks reaches the maximum, the task will be rejected
  • The thread exists until shutdown is called

A FixedThreadPool has a fixed number of core threads and no non-core threads. KeepAliveTime is set to 0L, which means that redundant threads are terminated immediately. KeepAliveTime is invalid because no extra threads are created; The task queue uses an unbounded blocking queue called LinkedBlockingQueue (the default size is integer.max_value).

Advantages and disadvantages of FixedThreadPool thread pools

Advantages: The number of fixed threads is relatively controllable

Disadvantages: Tasks with high concurrency are executed slowly and wait in queues.

Usage scenarios

Tomcat worker thread

3.2.3 ScheduledThreadPool

  • Set the delay time and perform it regularly
  • Idle threads are reserved

When performing ScheduledThreadPoolExecutor scheduleAtFixedRate or scheduleWithFixedDelay method, A task wrapper class ScheduledFutureTask that implements the RunnableScheduledFuture interface is added to DelayedWorkQueue and checks whether the running thread reaches the core thread count corePoolSize. If not, create a new thread and start it. Instead of executing the task immediately, go to the DelayedWorkQueue and fetch the task wrapper class ScheduledFutureTask, and then execute the task. If the running thread reaches corePoolSize, the task is added to the DelayedWorkQueue; DelayedWorkQueue sorts the tasks, with the first executed tasks placed at the front of the queue. After the task is executed, the variable time in ScheduledFutureTask is changed to the time to be executed next time and put back in the DelayedWorkQueue.

3.3 Rejection Policy

strategy instructions
AbortPolicy When a task is added to the thread pool is refused, it will throw RejectedExecutionException anomalies.
CallerRunsPolicy When a task is rejected when added to the Thread pool, the rejected task is processed in the Thread pool that is currently running in the Thread pool.
DiscardOldestPolicy When a task added to the thread pool is rejected, the thread pool discards the oldest unprocessed task in the wait queue, and then adds the rejected task to the wait queue.
DiscardPolicy When a task added to the thread pool is rejected, the thread pool discards the rejected task.

4 demonstrates

The resources

Thread pool ScheduledThreadPool learning blog.csdn.net/qq_36299025…

Comparison and use of five thread pools

www.jianshu.com/p/135c89001…

Other information

Java priority queue DelayedWorkQueue principle analysis www.jianshu.com/p/587901245…

SynchronousQueue will principle explanation – fair model www.cnblogs.com/dwlsxj/p/Th…

SynchronousQueue will principle explanation – not fair model www.cnblogs.com/dwlsxj/p/sy…