Thread pool principle of Java concurrency thread pool (1)_ Power node Java college sorting

Introduction to ThreadPoolExecutor

ThreadPoolExecutor is a thread pool class. For thread pools, It can be popularly understood as "A thread collection that stores a certain number of threads. If a thread is allowed to run at the same time, the number of threads allowed to run at the same time is the capacity of the thread pool. When the threads added to the thread pool exceed its capacity, some threads will block and wait. The thread pool will manage the threads added to the thread pool through corresponding scheduling policies and rejection policies 。 "

ThreadPoolExecutor data structure

The data structure of ThreadPoolExecutor is shown in the following figure:

Each data is displayed in ThreadPoolExecutor Definitions in Java are as follows:

1. workers

Workers is a HashSet < work > type, that is, it is a worker set. A worker corresponds to a thread, that is, the thread pool contains "a thread collection" through workers. When the worker's corresponding thread pool is started, it will execute tasks in the thread pool; After executing a task, it will take a blocked task from the blocking queue of the thread pool to continue running. The role of wokers is that the thread pool realizes "allowing multiple threads to run at the same time".

2. workQueue

Workqueue is of type BlockingQueue, that is, it is a blocking queue. When the number of threads in the thread pool exceeds its capacity, the thread will enter the blocking queue and wait for blocking. Through workqueue, the thread pool realizes the blocking function.

3. mainLock

Mainlock is a mutually exclusive lock, which enables mutually exclusive access to the thread pool.

4. Corepoolsize and maximumpoolsize

Corepoolsize is the core pool size and maximumpoolsize is the maximum pool size. Their role is to adjust the "number of threads actually running in the thread pool". For example, when a new task is submitted to the thread pool (through the execute method)-- If the number of threads running in the thread pool is < corepoolsize at this time, a new thread is created to process the request-- If the number of threads running in the thread pool is > corepoolsize, but < maximumpoolsize; A new thread is created only when the blocking queue is full. If you set the same corepoolsize and maximumpoolsize, a fixed size thread pool is created. Setting maximumpoolsize to a basic unbounded value (such as integer.max_value) allows the pool to accommodate any number of concurrent tasks. In most cases, the values of core pool size and maximum pool size are set when creating a thread pool; however, you can also use setcorepoolsize (int) and setmaximumpoolsize (int) to change dynamically.

5. poolSize

Poolsize is the actual size of the current thread pool, that is, the number of tasks in the thread pool.

6. Allowcorethreadtimeout and keepalivetime

Allowcorethreadtimeout indicates whether to allow "threads to survive when idle"; Keepalivetime is when the thread pool is idle. After keepalivetime, idle threads will be terminated.

7. threadFactory

Threadfactory is a threadfactory object. It is a thread factory class, "thread pool creates threads through threadfactory".

8. handler

Handler is of type rejectedexecutionhandler. It is the handle to the "thread pool rejection policy", that is, "when a task is added to the thread pool and the thread pool rejects the task, the thread pool will handle it through the handler".

To sum up, the thread pool manages the "thread collection" through workers. After each thread is started, it will execute the tasks in the thread pool; When a task is completed, it will take the task out of the blocking queue of the thread pool to continue running. Blocking queue is a queue for managing thread pool tasks. When a task added to the thread pool exceeds the capacity of the thread pool, the task will enter the blocking queue and wait.

Thread pool scheduling

Let's look at the scheduling strategy of tasks in the thread pool through the following figure to deepen our understanding of thread pool.

Figure-01:

Figure-02:

explain:

In figure-01, there are n tasks in the thread pool. "Task 1", "task 2" and "task 3" are executing, while "task 3" to "task n" are waiting in the blocking queue. In the workers collection, the workers collection contains three workers. Each worker corresponds to a thread thread. The thread thread processes one task at a time. After a task is processed in the workers collection, a task will be taken from the blocking queue to continue execution, as shown in figure-02. Figure-02 shows that after "task 1" is processed, the thread pool takes "task 4" out of the blocking queue and puts it into workers for processing.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>