Get the App
SLTechnology News&Howtos  ›  Development  › 

Summary of comprehensive knowledge points of Java thread pool

Shulou Source: shulou.com Published: 2022-06-02 20:59:55 09月14日 Update

This article introduces the relevant knowledge of "Java thread pool comprehensive knowledge point summary". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Principle

The principle of thread pool is very simple, which is summarized here in terms of processing flow:

The thread pool determines whether all the threads in the core pool are executing the task, and if not, create a new thread to execute the task.

If the core thread pool is full, store the new task in the work queue

If the work queue is full and the number of threads does not reach the thread pool limit, create a new thread to execute the task.

When the number of threads reaches the limit, a saturation policy is triggered to handle the task

Work queues are used to minimize the overhead of thread creation. Work queues are implemented with blocking queues.

Blocking queue

A blocking queue (BlockingQueue) is a queue that supports blocking insertion and removal of elements.

Blocking insertion: when the queue is full, block the thread that inserts the element until the queue is dissatisfied

Blocking removal: when the queue is empty, block the thread that removes the element until the queue is not empty

Principle: implemented using the notifier pattern. When a producer adds an element to a full queue, it blocks the producer. When a consumer removes an element, it notifies the producer that the current queue is available.

There are three types of blocking queues, which are:

Bounded blocking queues: ArrayBlockingQueue (array), LinkedBlockingQueue (linked list)

Unbounded blocking queues: LinkedTransferQueue (linked list), PriorityBlockingQueue (supports prioritization), DelayQueue (unbounded blocking queues that support delayed acquisition of elements)

Synchronous handover queue: SynchronousQueue

Bounded blocking queue

It mainly includes ArrayBlockingQueue (array) and LinkedBlockingQueue (linked list). The bounded queue size and the number of threads match each other. When the queue capacity is large and the number of threads is small, context switching can be reduced and cpu usage will be reduced, but throughput will be reduced.

Unbounded blocking queue

The more common one is LinkedTransferQueue. This is how FixedThreadPool is implemented. Unbounded blocking queues should be used with caution, because in some cases, it may cause a large number of tasks to pile up in the queue, causing memory to soar.

Synchronous handover queue

SynchronousQueue . Without storing the blocking queue of elements, each put operation must wait for a take operation, otherwise you cannot continue to add elements. Used to implement the CachedThreadPool thread pool.

The task queue mapping used by each thread pool is as follows:

Thread pool blocking queue

FixedThreadPoolLinkedBlockingQueueSingleThreadExecutorLinkedBlockingQueueCachedThreadExecutorSynchronousQueueScheduledThreadPoolExecutorLinkedBlockingQueue

Implementation class analysis

ThreadPoolExecutor is the implementation class of the Java thread pool and the core class derived from the Executor interface. The dependency diagram is as follows:

Here we have to mention the Executor framework, which consists of three parts, as follows:

Mission. Interfaces to be implemented for the task to be performed: Runnable and Callable

Mission carried out. That is, the above core interface Executor and the inherited ExecutorService. ExecutorService derives the following two classes: ThreadPoolExecutor: thread pool core implementation class; ScheduledThreadPoolExecutor: used to do timing tasks

The result of asynchronous calculation. Interface Future and the FutureTask class that implements the Future interface. Thread pool creation

New ThreadPoolExecutor (corePoolSize, maximumPoolSize, keepAliveTime, milliseconds, runnableTaskQueue, handler)

The construction method is as follows:

Public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {if (corePoolSize < 0 | maximumPoolSize

Tags: Thread task queue block work element core process quantity interface method idle knowledge principle parameter state policy saturation size Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MySQL OPPO Reno Huawei vpn