Discussion on some other knowledge points about thread pool


Preface

This blog records several types of task queues about thread pools, what are the commonly used task queues, how to determine the number of threads in a thread pool, and several commonly used thread pool creation methods.


1. Several task queues of thread pools

The commonly used ones are ArrayBlockingQueue and LinkedBlockingQueue

1.ArrayBlockingQueue

  • ArrayBlockingQueue is a bounded array task queue based on FIFO (first in first out task queue)

Insert image description here
As shown in the figure, the constructor of ArrayBlockingQueue has a capacity parameter that must be passed, so it is a bounded task queue.

2.LinkedBlockingQueue

  • LinkedBlockingQueue is a task queue with no solution. The bottom layer is a singly linked list, which is also based on FIFO (first in first out task queue).

Insert image description here
It is unbounded by default and is the maximum value of Integer.

3.DelayedWorkQueue

  • It is a priority queue, which can ensure that every task dequeued is the one with the longest execution time in the current queue.

4.SynchronousQueue

  • A blocking queue that does not store elements, each insertion operation must wait for the next removal operation

2. How to determine the number of core threads?

  • 1. High concurrency and short task execution time. The (number of CPU cores + 1) principle is used to determine the number of core threads and reduce thread context switching.
  • 2. If the concurrency is not high and the task execution time is long, if it is an IO-intensive task (number of CPU cores * 2 + 1); if it is a computing-intensive task (number of CPU cores + 1)
  • 3. High concurrency and long business execution time. The key to solving this type of tasks lies not in the thread pool but in the overall architecture design. Seeing whether certain data in these businesses can be cached is the first step, and adding servers is the second. Step 1. As for the settings of the thread pool, refer to the above principles for setting.

3. What are the types of thread pools?

1.newFixedThreadPool

  • Create a fixed-length thread pool to control the maximum number of concurrencies. Exceeding threads will wait in the queue.

Insert image description here
Insert image description here
Looking at the source code, we can see that the number of threads passed in is the maximum number of threads and the number of core threads. The active time of the emergency thread is 0 milliseconds. The task queue used is LinkedBlockingQueue, and the capacity of the task queue is not passed in, which means that the capacity is Integer. .MAX_VALUE, this is a risk

2.newSingleThreadExecutor

  • Create a single-threaded thread pool, which will only use the only worker thread to execute tasks, ensuring that all tasks are executed in the specified order (FIFO)

Insert image description here
Insert image description here
The number of core threads and the maximum number of threads of newSingleThreadExecutor are both 1, the survival time of the emergency thread is 0 milliseconds, and the task queue is LinkedBlockingQueue

3.newCacheThreadPool

  • Create a cacheable thread pool. If the length of the thread pool exceeds processing needs, idle threads can be flexibly recycled. If there is no way to recycle, create a new thread.

Insert image description here
Insert image description here
The number of core threads of newCacheThreadPool is 0, the maximum number of threads is Integer.MAX_VALUE, the emergency thread storage time is 60S, the task queue is SynchronousQueue, and the task queue is not stored.

4.newScheduledThreadPool

  • A thread pool that can execute delayed tasks and supports scheduled and periodic task execution

Insert image description here
The number of newScheduledThreadPool core threads needs to be defined. The maximum number of threads is Integer.MAX_VALUE, the emergency thread survival time is 0ms, and the task queue is DelayedWorkQueue.

4. Why is it not recommended to use Executor to create a thread pool?

  • 1. Thread pools are not allowed to be created using Executors, but through ThreadPoolExecutor. This processing method allows writers to be more clear about the operating rules of the thread pool and avoids the risk of resource exhaustion.
  • 2.FixedThreadPool and SingleThreadPool: The allowed request queue length is Integer.MAX_VALUE, which may accumulate a large number of requests, resulting in OOM (memory overflow)
  • 3.CachedThreadPool: The allowed number of threads to create is Integer.MAX_VALUE, which may create a large number of threads, resulting in OOM

Summarize

The knowledge points related to thread pools have come to an end. Next, I will continue to summarize security-related issues in concurrent programming, such as Synchronized locks, optimistic locks, pessimistic locks, and deadlocks.

Guess you like

Origin blog.csdn.net/qq_40187702/article/details/131627465