Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create a thread pool through ThreadPoolExecutor

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to create a thread pool through ThreadPoolExecutor. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

Before JDK1.5, developers had to manually create thread pools to save system overhead in the face of frequent thread scheduling scenarios (voiceover: really suffered a lot).

Starting with JDK1.5, Java provides an Excutors factory class for production line pool, which can help developers control threads effectively (voiceover: no need to build wheels, it's awesome).

(illustration: thread pool that JDK 1.8 can create with Excutors)

As shown in the figure above, Excutors provides a way to create thread pools that meet a variety of scenarios, so Java developers don't have to work hard to build wheels.

However, the Ali development protocol explicitly forces developers: thread pools are not allowed to be created using Executors, but through ThreadPoolExecutor.

However, students who often follow the source code will find that ThreadPoolExecutor is used behind the newFixedThreadPool () method, newSingleThreadExecutor () method, or newCachedThreadPool () method.

(illustration: behind the thread pool that JDK 1.8 can create with Excutors)

Through the screenshot of the above source code, we can clearly see that the above ways to create a thread pool are all encapsulation of the ThreadPoolExecutor class, so if you want to thoroughly grasp the thread pool, you must understand the ThreadPoolExecutor behind the thread pool.

one

Anatomy: constructor

The ThreadPoolExecute constructor is mentioned in many books or articles, so let's take a brief look at the specific meaning of each parameter.

Public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)

The argument definition of the constructor:

CorePoolSize: specifies the number of threads in the thread pool

MaximumPoolSize: specifies the maximum number of threads in the thread pool

KeepAliveTime: the survival time of idle threads when the number of threads in the thread pool exceeds corePoolSize

The unit of unit:keepAliveTime

WorkQueue: task queue for submitting tasks that have not yet been executed

ThreadFactory: thread factory, used to create threads, usually with the default

Handler: reject strategy, how to reject a task when there are too many tasks to deal with.

Except for workQueue and handler, most of the above parameters are easy to understand. Let's focus on the two parameters workQueue and handler.

The parameter BlockingQueue workQueue, which is used to store the queue for submitting tasks that have not been executed, is an object of the type of BlockingQueue interface, and is used to store Runnable objects.

The parameter RejectedExecutionHandler handler refers to what to do when the number of tasks exceeds the carrying capacity of the system. Among them, JDK provides four rejection strategies.

(with illustration: JDK 1.8 built-in rejection policy)

The four rejection strategies provided by JDK are summarized for a brief look.

two

Think about it: does using Executors lead to OOM?

After learning about the constructor of the ThreadPoolExecutor class, let's take a look at a specification that uses thread pools that is explicitly enforced in the Ali development manual.

For a clearer understanding, you might as well go into the source code and have a look. First of all, go into the source code of the newFixedThreadPool () method and find out.

As shown in the screenshot of the source code, the implementation of the newFixedThreadPool () method returns a thread pool that is the same size as corePoolSize and maximumPoolSize and uses the LinkedBlockingQueue task queue.

As shown in the source code of the LinkedBlockingQueue above, the default length of the queue is Integer.MAX_VALUE, so when the task is submitted frequently and the threads in the thread pool cannot handle it, the queue may swell rapidly, resulting in OOM.

Then go into the source code of the newSingleThreadExecutor () method and see if there is a new world.

As shown in the screenshot of the source code, in the implementation of the newSingleThreadExecutor method, both corePoolSize and maximumPoolSize are set to 1, return a single-threaded thread pool, and use the LinkedBlockingQueue task queue to have submitted tasks. Like the newFixedThreadPool () method, when tasks are submitted frequently and threads in the thread pool cannot handle them, the queue expands rapidly, resulting in OOM.

Finally, take a look at the source implementation of the newCachedThreadPool () method to find out.

As illustrated by the source code in the figure above, the newCachedThreadPool () method is implemented, which returns a thread pool with a value of corePoolSize 0 and a value of Integer.MAX_VALUE PoolSize, and uses SynchronousQueue as the task queue.

The SynchronousQueue queue is a queue that is submitted directly (the submitted task is not saved), so it always adds new threads to the thread pool to execute the task. When the task is finished, the idle thread will be reclaimed within 60 seconds because the corePoolSize is 0.

If a large number of tasks are submitted at the same time and the execution of the task is not so fast, the newCachedThreadPool () method will open a large number of threads for processing, which may quickly deplete the system's resources, resulting in OOM.

The above is how to create a thread pool through ThreadPoolExecutor. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report