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

What is the principle of Java thread pool implementation?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is the principle of Java thread pool implementation". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "what is the principle of Java thread pool implementation"?

Thread pool parameters

1. CorePoolSize (required): number of core threads.

2. MaximumPoolSize (required): maximum number of threads.

3. KeepAliveTime (required): how long the thread is idle. If this time is exceeded, the non-core thread will be reclaimed.

4. Unit (required): specify the time unit of keepAliveTime. Commonly used are: TimeUnit.MILLISECONDS (milliseconds), TimeUnit.SECONDS (seconds), TimeUnit.MINUTES (minutes).

5. WorkQueue (required): task queue. Runnable objects submitted through the thread pool's execute () method will be stored in the queue.

6. ThreadFactory (optional): thread factory. Usually use the default.

7. Handler (optional): reject policy. The saturation policy is executed when the number of threads reaches the maximum number of threads.

Tell the difference between the number of core threads and the maximum number of threads

Optional values for reject policy:

1. AbortPolicy (default): abandon the task and throw a RejectedExecutionException exception.

2. CallerRunsPolicy: the task is handled by the calling thread.

3. DiscardPolicy: abandon the task, but do not throw an exception. Custom handling can be done in conjunction with this mode.

4. DiscardOldestPolicy: abandon the earliest outstanding task in the queue, and then try to execute the task again.

II. Thread pool execution process

Briefly summarize the execution process of the downthread pool:

1. After a task is submitted to the thread pool, if the current number of threads does not reach the number of core threads, create a new thread and execute the new task. Note that after the new task is executed, the thread will not be destroyed.

2. If it is reached, determine whether the task queue is full or not. If not, put the task into the task queue.

3. if it is full, determine whether the current number of threads reaches the maximum number of threads; if not, create a new thread to execute the task. Note that if the number of threads in the thread pool is greater than the number of core threads, whenever a thread exceeds the idle time, it will be destroyed until the number of threads is not greater than the number of core threads.

4. If the maximum number of threads is reached and the task queue is full, the saturation policy will be executed

Three or four ready-made thread pools

If you don't want to use your own newline pool, you can use a ready-made one.

1. Fixed-length thread pool (FixedThreadPool)

Features: there are only core threads, the number of threads is fixed, and the task queue is listed as a bounded queue with linked list structure.

Application scenario: control the maximum number of concurrency of threads

2. Timing thread pool (ScheduledThreadPool)

Features: the number of core threads is fixed, the number of non-core threads is unlimited, and the task queue is listed as a delay blocking queue after the execution of idle 10ms.

Application scenarios: perform scheduled or periodic tasks.

3. Cacheable thread pool (CachedThreadPool)

Features: no core threads, unlimited number of non-core threads, reclaimed after idle for 60s, and task queue listed as a blocking queue that does not store elements.

Application scenarios: perform a large number of less time-consuming tasks.

4. Single-threaded thread pool (SingleThreadExecutor)

Features: there is only one core thread, which is no more than the core thread, and the task queue is listed as a bounded queue with linked list structure.

Application scenarios: operations that are not suitable for concurrency but may cause IO obstruction and affect the response of UI threads, such as database operations, file operations, etc.

Although the above four thread pools are convenient, the Alibaba specification clearly states that they are not recommended because they may cause memory overflow for the following reasons:

FixedThreadPool and SingleThreadExecutor: the main problem is that LinkedBlockingQueue is used in the stacked request processing queue, which may consume a lot of memory and lead to a serious memory overflow.

CachedThreadPool and ScheduledThreadPool: the main problem is that their maximum number of threads is Integer.MAX_VALUE, which may create a very large number of threads, resulting in a serious memory overflow.

Thank you for your reading, the above is the content of "what is the principle of Java thread pool implementation". After the study of this article, I believe you have a deeper understanding of what the principle of Java thread pool implementation is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Development

Wechat

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

12
Report