In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "use of ThreadPoolExecutor in java", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "the use of ThreadPoolExecutor in java" bar!
ThreadPoolExecutor is also known as thread pool. It is a development framework provided by Java for us to develop multithreaded programs. It can uniformly manage thread creation, destruction, optimization, monitoring, etc., and it is more convenient than using the original thread class directly when using the thread pool. Since the thread pool is so convenient, how on earth does it achieve the above functions? Let's first take a look at the flow chart of a thread when it is started with a thread pool.
The processing flow of the thread pool is as follows:
When we start a task with a thread pool, the thread pool first checks to see if the number of threads in the core thread pool has exceeded corePoolSize. If it does not exceed, create a new thread to execute the task. If it is exceeded, the currently executed task is added to the work queue of the thread pool, but before joining, it checks to see if the work queue is full, and if the work queue is full, then it checks to see if the maximum number of threads in the thread pool has been exceeded. If it is not exceeded, the thread is created to execute the task, and if the maximum number is exceeded, it is processed according to a policy that cannot be executed.
Thread pool creation: when creating a ThreadPoolExecutor, you will need to pass several necessary parameters. Let's take a closer look at what each parameter represents.
CorePoolSize (initialized idle threads): when we create a ThreadPoolExecutor object, we can use the corePoolSize parameter to set the number of initialization threads in the thread pool, that is, idle threads. When the number of threads in the thread pool is less than corePoolSize, the thread pool will recreate a new thread to process the task instead of directly using the idle threads in the thread pool.
MaximumPoolSize (maximum number of thread pools): the maximum number of threads allowed to be created by the thread pool. This is the maximum number of concurrency, that is, how many threads are allowed to execute at the same time in the thread pool.
KeepAliveTime (thread activity hold time): use this parameter to set the time for idle threads to wait for a new task when the number of threads in the thread pool is greater than corePoolSize. If the thread does not receive a new task during this time, the current thread is destroyed.
TimeUnit (units of thread activity hold time): optional units are days (DAYS), hours (HOURS), minutes (MINUTES), milliseconds (MILLISECONDS), microseconds (MICROSECONDS, 1/1000 milliseconds), and nanoseconds (NANOSECONDS, 1/1000 microseconds).
BlockingQueue (task queue in the thread pool), when the number of threads in the thread pool is greater than corePoolSize, the newly submitted task will be saved to the task queue. There are mainly four different task queues in the thread pool.
ArrayBlockingQueue: a task queue based on an array structure. This queue sorts tasks on a first-in-first-out basis.
LinkedBlockingQueue: a task queue based on a linked list structure. This queue also sorts tasks on a first-in-first-out basis. But the performance is usually higher than ArrayBlockingQueue.
SynchronousQueue: a task queue that does not store elements. Each insert operation must wait until another thread invokes the remove operation, otherwise the insert operation remains blocked.
PriorityBlockingQueue: is a queue of tasks with priority. Elements in this queue must be comparable.
ThreadFactory (): the thread factory where the thread is created.
RejectedExecutionHandler (saturation strategy): when the number of threads in the thread pool is greater than maximumPoolSize, the thread pool can no longer handle any tasks, and the thread pool throws an exception. The reason is that this policy defaults to AbortPolicy: it means that an exception is thrown when a new task cannot be processed. There are several other strategies:
AbortPolicy: throw an exception directly.
CallerRunsPolicy: only use the caller's thread to run the task.
DiscardOldestPolicy: discard the most recent task in the queue and execute the current task.
DiscardPolicy: don't deal with it, discard it.
Let's use specific code to explain the use of ThreadPoolExecutor in detail.
According to the above analysis, because we initialized 2 idle threads when we created the ThreadPoolExecutor object, and we added 2 to the thread pool, tasks that are currently executed by the core thread pool will not be added to the alignment. If I continue to submit tasks to the thread pool, we will have our newly submitted tasks in the queue because we have exceeded the number of corePoolSize we set.
Because the maximum number of threads in the thread pool we set is 3, which is the value of maximumPoolSize. If this value is exceeded and we do not change the corresponding saturation policy, an exception will be thrown at this time.
We found that the program did not report an error, because of what. This is because the parameter maximumPoolSize means that the maximum number of concurrency allowed by the thread pool is 3, that is, three threads can be executed at the same time, but let's not forget that there is a queue in the thread pool. What is stored in the queue is the task to be executed, but now it has exceeded the maximum number of concurrency, so the task in the queue can only wait for other tasks in the thread pool to finish before it can be executed. So the maximum number of tasks we are allowed to submit in the thread pool at this point is the number of maximumPoolSize plus queues. However, if we continue to add tasks to the thread pool, the thread pool will report an error, because there is no room to store new tasks and the queue is full, so the default policy to go through the saturation policy is to throw an exception.
Let's modify the saturation policy of the thread pool.
Introduction of related methods in thread pool
TaskCount: the number of tasks that need to be executed by the thread pool. Although we submitted five tasks to the thread pool, the fifth task was not performed by the thread pool, but was executed by ourselves by modifying the saturation policy. So this value returns a result of 4.
CompletedTaskCount: the number of tasks completed in the thread pool.
LargestPoolSize: the maximum number of threads ever created in the thread pool. That is, how many threads are executing at the same time, also known as the maximum number of concurrency.
GetPoolSize: the number of threads in the thread pool. If the thread pool is not destroyed, the threads in the thread pool will not be destroyed automatically.
GetActiveCount: the number of active threads.
Thank you for reading, the above is the content of "the use of ThreadPoolExecutor in java", after the study of this article, I believe you have a deeper understanding of the use of ThreadPoolExecutor 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.