Java Concurrent Programming: the use of thread pools
Java Concurrent Programming: the use of thread pools
In the previous article, we created a thread when using threads, which is very easy to implement, but there will be a problem:
If there are a large number of concurrent threads and each thread executes a task for a short time, the efficiency of the system will be greatly reduced because it takes time to create and destroy threads frequently.
So is there a way to make threads reusable, that is, after executing a task, they can continue to execute other tasks without being destroyed?
In Java, this effect can be achieved through thread pool. Today, let's explain the thread pool of Java in detail. First, we start with the method in the core ThreadPoolExecutor class, then describe its implementation principle, then give its use examples, and finally discuss how to reasonably configure the size of the thread pool.
The following is an outline of the contents of this article:
I ThreadPoolExecutor class in Java
II Deeply analyze the implementation principle of thread pool
III Use example
IV How to reasonably configure the size of thread pool
If there is anything wrong, please forgive me and welcome criticism and correction.
Please respect the author's labor achievements. Please indicate the original link for Reprint:
http://www.cnblogs.com/dolphin0520/p/3932921.html
I ThreadPoolExecutor class in Java
java. uitl. concurrent. The ThreadPoolExecutor class is the core class in the thread pool. Therefore, if you want to thoroughly understand the thread pool in Java, you must first understand this class. Let's take a look at the specific implementation source code of the ThreadPoolExecutor class.
Four constructors are provided in the ThreadPoolExecutor class: