Java Concurrency: Implementation and principle of thread pool part 6 thread pool (1)

Source: Internet
Author: User

Thread Pool data structure and thread Construction Method

As we have seen the source code of ThreadPoolExecutor, we can easily see the data structure of the ThreadPoolExecutor thread pool. Figure 1 describes the data structure.

Figure 1 ThreadPoolExecutor Data Structure

In fact, even if the data structure of ThreadPoolExecutor is not described in the preceding figure, we can guess its data structure according to the requirements of the thread pool.

The Thread pool must support concurrent execution of multiple threads, so there is a Collection of threads <Thread> to execute Thread tasks;
Asynchronous execution of tasks involved. Therefore, a Collection is required to cache the Collection of task queues <Runnable>;
Obviously, if multiple tasks are coordinated among multiple threads, a thread-safe task set is required, and blocking and timeout operations are also required, BlockingQueue is essential;
Since it is a thread pool, the starting point is to improve system performance and reduce resource consumption, so there is a limit on the size of the thread pool, so there needs to be a core thread pool size (number of threads) and the maximum thread pool size (number of threads), there is a count used to describe the current thread pool size;
If the thread pool size is limited, thread resources that are not used for a long time should be destroyed. In this way, a thread's idle time count is required to describe when the thread is destroyed;
As described above, the thread pool also has a life cycle. Therefore, a State is required to describe the current running status of the thread pool;
If the task queue in the thread pool has boundaries, a task denial policy is required to process too many tasks, at the same time, in the thread pool destruction stage, a task denial policy is required to handle newly added tasks;
The changes in the preceding thread pool size, thread idle status, and thread pool running status are not thread-safe. Therefore, a global lock is required) to coordinate these competitive resources;
In addition to the preceding data structure, ThreadPoolExecutor also has some statuses used to describe the running count of the thread pool, such as the number of tasks run in the thread pool and the maximum number of threads that have been reached, which are mainly used for debugging and performance analysis.
 

For ThreadPoolExecutor, a thread is a Worker object, which is bound to a thread. When the Worker execution is completed, the thread execution is completed. This topic describes in detail how the thread runs in the thread pool.

Since it is a thread pool, we should first study the thread constructor.

Public interface ThreadFactory {
Thread newThread (Runnable r );
}
 

ThreadPoolExecutor uses a thread factory to construct a thread. The Thread pool submits a task Runnable and then executes it in a Thread. ThreadFactory is responsible for creating a new Thread.

In J. U. C, there is a general thread factory java. util. concurrent. Executors. DefaultThreadFactory. Its construction method is as follows:

Static class DefaultThreadFactory implements ThreadFactory {
Static final AtomicInteger poolNumber = new AtomicInteger (1 );
Final ThreadGroup group;
Final AtomicInteger threadNumber = new AtomicInteger (1 );
Final String namePrefix;
DefaultThreadFactory (){
SecurityManager s = System. getSecurityManager ();
Group = (s! = Null )? S. getThreadGroup ():
Thread. currentThread (). getThreadGroup ();
NamePrefix = "pool-" +
PoolNumber. getAndIncrement () +
"-Thread -";
}
Public Thread newThread (Runnable r ){
Thread t = new Thread (group, r,
NamePrefix + threadNumber. getAndIncrement (),
0 );
If (t. isDaemon ())
T. setDaemon (false );
If (t. getPriority ()! = Thread. NORM_PRIORITY)
T. setPriority (Thread. NORM_PRIORITY );
Return t;
}
}
 

In this thread factory, all threads in the same thread pool belong to the same thread group, that is, the thread group that creates the thread pool, the thread pool names are all "pool-<poolNum>-thread-<threadNum>". poolNum indicates the number of threads in the thread pool and threadNum indicates the number of threads in the thread pool. In this way, we can easily see the number of thread pools in the system and the number of threads in the thread pool. In addition, all threads in the thread pool are converted to non-Background threads by default, so that the main thread does not exit JVM directly when exiting, but waits until the thread pool ends. Another point is that all threads in the thread pool are adjusted to the same level by default. In this way, from the operating system perspective, all systems are fair and won't lead to competition accumulation.

Thread lifecycle in the thread pool

After a thread Worker is constructed, it starts to run. Below is a simple logic for thread execution.

Private final class Worker implements Runnable {
Private final ReentrantLock runLock = new ReentrantLock ();
Private Runnable firstTask;
Thread thread;
Worker (Runnable firstTask ){
This. firstTask = firstTask;
}
Private void runTask (Runnable task ){
Final ReentrantLock runLock = this. runLock;
RunLock. lock ();
Try {
Task. run ();
} Finally {
RunLock. unlock ();
}
}
Public void run (){
Try {
Runnable task = firstTask;
FirstTask = null;
While (task! = Null | (task = getTask ())! = Null ){
RunTask (task );
Task = null;
}
} Finally {
WorkerDone (this );
}
}
}
 

When submitting a task, if you need to create a thread (when to be discussed in the next section), call the thread factory to create a thread and bind the thread to the Worker work queue. Note that the Worker queue is constructed with a task Runnable. Therefore, a Worker is always bound to a task to be executed. In other words, the premise for creating a thread is that it is necessary to create a thread (the number of tasks has exceeded the thread or force a new thread to be created, and the subsequent chapter will analyze the force to create a new thread ), A bunch of Idle threads will not be created for no reason. This is a way to save resources.

Once the thread pool starts the thread (call the thread run () method, then, the Worker in the thread work queue starts to execute from 1st tasks (this shows the benefits of passing a task when constructing a Worker). Once the execution of 1st tasks is completed, the next task is retrieved from the task queue in the thread pool for execution. This occurs until the thread pool is closed or the task throws a RuntimeException.

It can be seen that the basic principle of the thread pool is actually very simple, nothing more than starting some threads in advance, the thread enters the endless loop state, each time a task is obtained from the task queue for execution, until the thread pool is closed. If a thread terminates due to an exception in executing a task, a new thread is created. This is repeated.

In fact, the thread pool principle seems simple, but complicated is various strategies, such as when to start a thread, when to terminate, suspend, wake up a thread, and the blocking and timeout of the task queue, the lifecycle of the thread pool and the task denial policy. These policy issues will be studied in the next section.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.