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 Java thread pool?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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

As programmers, we are all too familiar with threads. Multithreading is an advanced knowledge of Java, and it is not easy to have too many holes with many threads. Creating a thread is also a "heavy" operation. The statement to create the thread is new Thread (). At first glance, it looks like new an object.

Yes, new has an object, but not only does it allocate a piece of memory in the heap like a normal object, it also needs to call the operating system kernel API, and then the operating system allocates some resources to the thread. So threads are "heavier" than ordinary objects. So we want to avoid frequent creation and destruction of threads, but also have to control the number of threads. Thread pools are used to accomplish this mission.

Therefore, multithreading is inseparable from the thread pool, so to master multithreaded programming, the understanding of thread pool is essential.

The thread pool is designed to adopt the producer-consumer model. The threads in the thread pool are consumers, and the task we give to the thread pool is the producer. It can be understood that the thread pool is the railway station ticket hall, and the thread in the thread pool is the window staff of the railway station ticket hall. We go to buy tickets or refund and change tickets, that is, to give the window staff the task of production, and then the window staff help us handle the business. That is, consumption.

Usually we use ThreadPoolExecutor to create a thread pool, and I found the constructor with the most parameters in it.

1 、 corePoolSize

Literally, the size of the core pool is the minimum number of threads in the thread pool. It should be noted here that when initializing the thread pool, unless prestartAllCoreThreads or prestartCoreThread are called, all core threads or one thread are pre-created before no task arrives. Otherwise, after the thread pool is initialized, there is no thread before the task comes in. Threads are created only when the task comes.

So the number of cores held here refers to the number of threads that will not be recycled after so many threads have been created in the thread pool, and threads that exceed the corePoolSize will be recycled after a certain period of time.

However, java1.6 has added an allowCoreThreadTimeOut (boolean value) method that when set to true, all threads will be reclaimed in timeout, including core threads.

2 、 maximumPoolSize

The maximum number of threads, that is, the maximum number of threads available in the pool. That is, all the windows of the ticket office of the railway station are served by employees. Especially in the holidays, basically the window will be open.

3 、 keepAliveTime 、 TimeUnit

KeepAliveTime is the time to live, and TimeUnit is the unit of time to indicate whether the number of keepAliveTime is seconds or milliseconds, and so on.

These two parameters are that when the number of threads in our thread pool exceeds corePoolSize, if a thread has been idle for such a long time as keepAliveTime, then the idle thread will be reclaimed, just like the rush hour is over, and the ticket office window can be closed. It can't be empty and open so many windows. It's a waste.

4 、 workQueue

The work queue is the blocking queue. The queue stores the Runnable that the thread needs to execute, that is, the task. It corresponds to us waiting in line at the ticket office.

5 、 threadFactory

Translated by name is a thread factory, that is, we can set up a factory, and then customize how to create a thread, such as giving a thread a name set and so on. The thread pool then creates the thread as defined by the factory. That is, if you do not set the name of the thread, the thread name may be something like thread-1, which is not convenient for us to troubleshoot the problem, so it is better to give a name to identify it.

6 、 handler

This is the reject policy, that is, when all the threads in the thread pool are performing tasks and the work queue (bounded queue) is full, the reject policy will be implemented when any more tasks are submitted. ThreadPoolExecutor provides four rejection strategies

1. ThreadPoolExecutor.AbortPolicy ()

Is the default reject policy and throws a RejectedExcecutionException.

2. ThreadPoolExecutor.CallerRunsPolicy ()

Let the thread submitting the task perform the task on its own. It seems to make sense to do so.. I don't have time. You can do it yourself.

3. ThreadPoolExecutor.DiscardOldestPolicy ()

Discard the oldest task, the top task in the work queue, and then add the new task to the work queue. It's really not fair.

4. ThreadPoolExecutor.DiscardPolicy ()

Simply discard the task and do not throw any exceptions. Pretend not to see the series.

In addition to these four, you can also customize the rejection policy, and it is recommended to customize the rejection policy. Because it is more friendly, it can be set to operations such as service degradation and so on.

Be careful

The Java concurrent package also provides Executors, which allows you to quickly create thread pools, but Executors is not recommended. Because Executors creates thread pools using unbounded queue LinkedBlockingQueue by default, it is easy to OOM under high load. Therefore, it is recommended to use bounded queues.

Summary

So thread pool is the implementation of producer-consumer model. Thread pool restricts the number of threads and avoids frequent thread creation and destruction. The existence of the work queue makes the task proceed in an orderly and perfect way!

Thank you for your reading, the above is the content of "what is Java thread pool", after the study of this article, I believe you have a deeper understanding of what Java thread pool 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