ThreadPoolExecutor Analysis of java concurrency, threadpoolexecutor

Source: Internet
Author: User
Tags throw exception

ThreadPoolExecutor Analysis of java concurrency, threadpoolexecutor

ThreadPoolExecutor Analysis of java concurrency

ThreadPoolExecutor thread pool is the most commonly used thread pool processing tool. It fully understands the implementation principle of the thread pool and allows us to effectively tune and process parameters when encountering related problems, this improves the overall performance and throughput of the program. This article relies on jdk1.8, but does not discuss the implementation details of the code. It should be noted that the ctl attribute combines the two attributes into an int field for record, high 3-bit recording thread pool status, low 29-bit recording thread pool count. In this case, the benevolent sees the Wise, and the complexity increases. How much can the performance be improved? When reading this article, you can think of int as an array of 32 characters in length.

"One center, two basic points", we have recited countless political science contents in the student age, and also applied to ThreadPoolExecutor. The center is the BlockingQueue as the data center, and the corePoolSize and maximumPoolSize are two basic points. As long as we fully understand their role, we seize the key to understanding ThreadPoolExecutor. Below I will describe the actual meaning of these fields using the image method

 

 

The entire large box represents the ThreadPoolExecutor class. We can regard orePoolSize and maximumPoolSize as two thresholds, the behavior of the thread pool varies in different stages. The poolSize is the number of currently running thread pools (This field does not exist in ThreadPoolExecutor. This field exists in jdk1.6, there are currently 29 digits after ctl). works stores the current working thread and blockQueue stores our task unit. Assume that the thread pool has been initialized and the internal status is waiting for our input. Then we execute the execute method. So

1. If poolSize <corePoolSize, the thread pool will directly create a new thread for task execution,

 

 

Move the poolsize pointer up and store the new thread in works for later use by the task.

2. If poolSize is greater than or equal to corePoolSize, in either case, if blockqueue of A is not filled up, data is directly filled in the queue,

 

 

B. If the queue is full, a new thread is created for task execution,

 

 

The second step is actually the core and most essential part of the thread pool. Under normal circumstances, if the task is submitted smoothly to the thread pool and each parameter is properly designed, the thread pool is always running with the number of corePoolSize threads. Therefore, it is very important to set the corePoolSize value. Also, refer to the blockqueue size configuration. CorePoolSize is too large, causing unnecessary waste. Being too small will cause constant fluctuations in the thread pool. Blockqueue size is also very important. We do not recommend that you do not specify the size of Blockqueue. This will use the maximum value of int for initialization. When the system is busy, it will generate a large backlog of tasks, and it is prone to memory overflow, the last life-saving straw of the thread pool is also unplugged. The above is a stable situation. If there is a peak value, it doesn't matter. At this time, maximumPoolSize starts to take effect. For example, the key here is that the newly created thread will not be destroyed immediately, it will join the previous thread pool to consume the tasks in blockqueue with everyone. In the worst case of the system, you can create maximumPoolSize threads, so that everyone can work together to survive this busy time. This is the same as our daily life. If we want to complete too many tasks, the project team will immediately increase manpower and improve the overall combat effectiveness by recruiting people and calling other groups.

3. If poolSize> = maximumPoolSize, the thread pool has reached the maximum load. What should I do at this time.

 

 

To be honest, the thread pool can no longer be used at this time. If he cannot do it, the problem will be thrown to you when he doesn't do it. At this time, you need to select a strategy to deal with this situation, of course, the most common throw exception, the thread pool has almost crashed to this extent. RejectedExecutionHandler is the final emergency measure. Of course, we need to handle this situation by ourselves.

4. If we don't get through on March 3, it's estimated that the system is down. Let's assume that we have worked together to overcome the difficulties. Now the question is how to recycle such multithreading? It is obviously not our style to ignore the content. When the blockqueue queue is empty and poolSize> corePoolSize, the thread starts to recycle until only corePoolSize threads are left. Everything has been restored. At the beginning, it seems that nothing has ever happened. Heroes have always been like this. When the chaos begins, the war is over, and the war ends, the war ends, and the war ends.

So far, the entire ThreadPoolExecutor discussion has been completed. The core idea is that a few people will work slowly when they are not in a rush. Once a big task suddenly arrives, they will find a few more people to do it. At this time, some students may have asked us whether we have the opportunity to show our own performance, that is, what extensions does he have? Yes. For example, in the ScheduledThreadPoolExecutor class, his core idea is to change the blockqueue queue to a priority queue sorted by time, and then complete the timer scheduling. At the same time, we can also perform some custom processing before and after the execution of our task, as long as the beforeExecute and afterExecute interfaces are implemented during inheritance.

Java enterprise-level general permission security framework source code SpringMVC mybatis or hibernate + ehcache shiro druid bootstrap HTML5

[Download java framework source code]

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.