Exploring the principle of threadpoolexcutor analyzing the implementation principle of AQS through reentrantlock

an introduction to

Thread pool (English: thread pool): a thread usage mode. Too many threads will bring scheduling overhead, which will affect cache locality and overall performance. The thread pool maintains multiple threads, waiting for the supervisor to allocate concurrent tasks. This avoids the cost of creating and destroying threads when processing short-time tasks. The thread pool can not only ensure the full utilization of the kernel It can also prevent excessive scheduling. The number of available threads should depend on the number of available concurrent processors, processor cores, memory, network sockets, etc. For example, it is appropriate to take the number of CPUs + 2 as the number of threads. Too many threads will lead to additional thread switching overhead.

The thread pool in Java is implemented with the ThreadPoolExecutor class This paper analyzes the execution principle of thread creation, management and background task scheduling in this class based on the source code of this class.

First look at the class diagram of the process pool:

The purpose of the above figure is to let you know the relationship between thread pool related classes, at least make you familiar, and you won't feel afraid when you see it in the future.

Executor framework interface

Executor framework is a framework that invokes, schedules, executes and controls asynchronous tasks according to a set of execution policies. Its purpose is to provide a mechanism to separate "task submission" from "how tasks run".

The following is the ThreadPoolExecutor class diagram. Executors is actually a tool class, which provides many static methods that return different thread instances according to the user's choice.

It can also be seen from the above figure that ThreadPoolExecutor is the core of thread pool.

J. There are three executor interfaces in U.C:

In fact, some design ideas can be seen through these interfaces. The name of each interface exactly matches its task. Because there is only one method in the executor, it will not be put into other interfaces. This is also an important single principle.

ThreadPoolExecutor analysis

Before specifically analyzing the ThreadPoolExecutor operation logic, first look at the following flow chart:

This figure is a summary of the whole operation process of ThreadPoolExecutor. The core logic of the whole source code is summarized as follows:

Next, we will enter the source code analysis to deeply understand the design idea of ThreadPoolExecutor.

Constructor

Let's start with the constructor:

During the above analysis, a worker class was mentioned. Some students who are not very familiar with the source code may be a little unclear. Let's take a look at the source code of the worker:

First, the worker inherits the AQS and implements the runnable interface, indicating that the worker itself is also a thread. Then look at its constructor. It can be found that there are two internal attribute variables, runnable and thread instances. In fact, this class encapsulates the passed attributes and adds the logic of obtaining locks (inheriting AQS). For details, please refer to the article: analyze the implementation principle of AQS through reentrantlock

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>