In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use the thread pool in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use thread pool in Java".
Among the thread pool-related utility classes provided by Java, the core is ThreadPoolExecutor.
The constructor of ThreadPoolExecutor is very complex. As shown in the following code, the most complete constructor has seven parameters.
ThreadPoolExecutor (int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler)
Let's describe the meaning of these parameters one by one. You can compare the thread pool class to a project team, and the thread is a member of the project team.
CorePoolSize: represents the minimum number of threads held by the thread pool. Some projects are very idle, but we can't get rid of all the people, at least leave corePoolSize individuals to hold their ground.
MaximumPoolSize: represents the maximum number of threads created by the thread pool. When the project is very busy, it needs to be added, but it can not be added indefinitely, at most, it can be added to maximumPoolSize individuals. When the project is idle, it will be withdrawn, and at most it can be withdrawn to corePoolSize individuals.
KeepAliveTime & unit: it is mentioned above that projects increase or decrease people according to their availability, so in the programming world, how do you define busy and idle? Quite simply, if a thread does not execute a task for a period of time, it means it is idle. KeepAliveTime and unit are the parameters used to define this "period of time". That is, if a thread has been idle keepAliveTime & unit for so long and there are more threads in the thread pool than corePoolSize, then the idle thread will be reclaimed.
WorkQueue: work queue.
ThreadFactory: with this parameter you can customize how to create a thread, for example, you can assign a meaningful name to the thread.
Handler: with this parameter, you can customize the rejection policy of the task. If all the threads in the thread pool are busy and the work queue is full (provided that the work queue is bounded), then the task is submitted and the thread pool refuses to receive it. As for the rejection policy, you can specify it through the parameter handler. ThreadPoolExecutor already provides the following four strategies.
CallerRunsPolicy: the thread that submits the task executes the task itself.
AbortPolicy: the default reject policy, which is throws RejectedExecutionException.
DiscardPolicy: discards the task directly without any exception thrown.
DiscardOldestPolicy: to discard the oldest task is to discard the first task to enter the work queue, and then add the new task to the work queue.
Java also added the allowCoreThreadTimeOut (boolean value) method in version 1.6, which allows all threads to support timeouts, which means that if the project is idle, all members of the project team will be removed.
Thank you for reading, the above is the content of "how to use the thread pool in Java". After the study of this article, I believe you have a deeper understanding of how to use the thread pool in Java, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.