Deep introduction to multithreading (6) Design and implementation of parallel package Thread Pool

Source: Internet
Author: User
Navigation of this series of articles

Java multithreading (1)-method join

Java multithreading (2)-EDT in Swing (event distribution thread)

In-depth introduction to multithreading (3)-Future asynchronous mode and its implementation in the JDK1.5Concurrent package

Introduction to multithreading (4) some ideas on the problem of CachedThreadPool OutOfMemoryError

Deep introduction to multithreading (5) use the parallel package thread pool as an example to describe the design requirements and usage of the thread pool

Deep introduction to multithreading (6) Design and implementation of parallel package Thread Pool

 

Next, I will introduce the design requirements and usage of the thread pool in the previous article. Here we will analyze the design of the ThreadPoolExecutor implementation class of the thread pool in the Concurrent package. Is it meaningless, what else can we analyze when everyone else does it? Of course, it is to improve our own design capabilities. In my opinion, we should be able to learn from our predecessors in practice.

Summarized Design Ideas (such as design patterns) can be integrated and used according to the specific context. On the other hand, analysis of excellent framework design and implementation is also a good way, of course, JDK design is the best learning materials. There are too many things to talk about, and the main points of design are as follows:

Throughout JDK1.5, we implemented our own thread pool or the thread pool provided by Concurrent. In the thread pool, we need two containers to maintain the threads in the thread pool and submit them to the tasks in the thread pool.

How can a thread pool be associated with a Task? What types of tasks can be submitted for execution in the thread pool. Therefore, we need to define an interface to separate the thread pool from the specific

The coupling relationship between tasks. ThreadPoolExecutor can accept specific tasks that implement the Runnable interface or Callable interface (in fact, it is finally assembled as the Runnable interface.

The thread in the thread pool executes tasks from the Task queue.

The above are the key points of thread pool design.

In threadpoolexecutor, there is an internal worker class that implements runnable, that is, the thread in the thread pool. It is self-evident that its run method is from the task queue

Take the task, call the run method of the task (which implements the runnable interface), execute the task, and so on until there is no task in the queue. View

{
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
Get_larger (this)
}
}
}
} "Src =" http://p.blog.csdn.net/images/p_blog_csdn_net/hemingwang0902/EntryImages/20090916/7.jpg "alt =" Deep introduction to multithreading (6) Analysis of parallel package Thread Pool Design and Implementation "> understand the thread creation and execution of the task process, let's take a look at how threadpoolexecutor triggers thread creation in the thread pool? When to start

What if the thread executes the task? Before that, let's first explain that two attributes describing the number of threads in the thread pool are introduced in threadpoolexecutor, corepoolsize and maximumpoolsize.

Number of threads in the thread pool during corepoolsize initialization.

Maximumpoolsize indicates the maximum number of threads in the thread pool. When a task cannot be inserted into the task queue and the number of threads in the thread pool reaches maximumpoolsize, the reject policy is enabled,

Too many tasks are reject.

It indicates that when we create a thread pool and submit a Task, the ThreadPoolExecutor first checks whether it has reached the corePoolSize. If it does not exist, it creates a thread to increase the throughput. If the number of tasks exceeds, insert the Task into the Task queue.

If the insertion fails, the Task queue is full, and whether the attempt reaches the maximumPoolSize is reached. If not, the external thread is created to handle the modification.

Task to reduce the Task failure rate.

If maximumPoolSize is reached, sorry, only Reject is supported.

{
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
Get_larger (this)
}
}
}
} "Src =" http://p.blog.csdn.net/images/p_blog_csdn_net/hemingwang0902/EntryImages/20090916/8.jpg "alt =" Deep introduction to multithreading (6) Analysis of parallel package Thread Pool Design and Implementation ">

This is actually a policy designed by the thread pool to process tasks. You can understand the advantages of this strategy in detail.

Now we have basically explained the design of ThreadPoolExecutor. Is the design idea the same as that of our own design thread pool? But it is more perfect and perfect in actual implementation!

If you are interested, let's take a look at how ThreadPoolExecutor elegantly Shutdown. These design implementation details can be applied in our practice.

 

Blog Source:Http://www.cqzol.com/programming/580437.html

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.