In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use ThreadPoolTaskExecutor". 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 ThreadPoolTaskExecutor.
The use of ThreadPoolTaskExecutor
ThreadPoolTaskExecutor is usually used when we need to implement concurrent, asynchronous, and other operations. Here is a brief summary of its use.
Springboot configuration
Submit a task
Tasks with no return value use execute (Runnable)
Tasks with return values use submit (Runnable)
Processing flow
When a task is submitted to the thread pool, first check to see if all the core threads of the thread pool are executing the task, or not select a thread to execute the task, and perform the second step.
Check to see if the core thread pool is full, create a thread to execute the task if you are dissatisfied, otherwise perform step 3.
Check to see if the task queue is full, and if you are dissatisfied, store the task in the task queue, otherwise perform step 4.
Check to see if the thread pool is full and create a thread to execute the task, otherwise it will deal with tasks that cannot be performed according to policy.
In ThreadPoolExecutor, it is shown as:
If the number of threads currently running is less than corePoolSize, then create a thread to execute the task (you need to acquire a global lock for execution).
If the running thread is greater than or equal to corePoolSize, task is added to the BlockQueue.
If the number of threads created is greater than the maximum capacity of the BlockQueue, create a new thread to perform the task.
If the creation of a thread causes the current number of running threads to exceed maximumPoolSize, the task is rejected according to the saturation policy.
Close the thread pool
Call shutdown or shutdownNow, both of which do not accept new tasks, and interrupt the thread by calling the interrupt method that you want to stop the thread. It is possible that the thread will never be interrupted, except that shutdownNow first sets the state of the thread pool to STOP, then attempts to stop all threads (which may result in some tasks not being finished), and then returns the list of unexecuted tasks. Shutdown, on the other hand, simply sets the state of the thread pool to shutdown, then interrupts all threads that do not execute the task, and finishes the rest of the task.
Configure number of threads
If it is a CPU-intensive task, then the number of threads in the thread pool should be as small as possible, usually the number of CPU + 1 thread.
For IO-intensive tasks, the number of threads in the thread pool can be large, such as the number of 2*CPU.
For mixed tasks, if you can split them, you can improve your execution efficiency by splitting them into CPU-intensive and IO-intensive tasks; if you can't split them, you can adjust the number of threads in the thread pool according to the actual situation.
Monitor thread pool status
Common state
TaskCount: the number of tasks that the thread needs to perform.
CompletedTaskCount: the number of tasks completed by the thread pool during operation.
LargestPoolSize: the maximum number of threads ever created by the thread pool.
GetPoolSize: gets the number of threads in the current thread pool.
GetActiveCount: get the number of active threads
By inheriting the thread pool, overriding the beforeExecute,afterExecute and terminated methods to obtain the running condition of the thread before the thread executes the task, the end of the thread execution task, and the thread termination, and adjust the number of threads in the thread pool according to the specific situation.
ThreadPoolTaskExecutor configuration issu
Recently, there is a strange problem online. ThreadPoolTaskExecutor is used to handle subsequent service calls. There is no problem at the beginning of running ThreadPoolTaskExecutor to handle subsequent service calls, but after a period of time, it is found that subsequent services have not been called, resulting in extremely serious consequences.
The details about ThreadPoolTaskExecutor in spring are as follows
Then we have to understand the Executor architecture under the java.util.concurrent package.
Recall how the thread pool works
If fewer threads are currently running than corePoolSize, create a new thread to execute the task (requires a global lock)
If the running thread is equal to or more than corePoolSize, the task is added to BlockingQueue
If the task cannot be joined to BlockingQueue (the queue is full), create a new thread to process the task (need to obtain a global lock)
If creating a new thread will cause the currently running thread to exceed the maxiumPoolSize, the task will be rejected and call the
RejectedExecutionHandler.rejectedExecution () method
Test scenario 1
First, comment on the line of queueCapacity
Task:
Public class CustomRunnable implements Runnable {private int id; public CustomRunnable (int id) {this.id = id;} @ Override public void run () {try {System.out.println ("begin execute" + Thread.currentThread () .getName () + "- task id:" + id); String rs = ClientUtil.get ("http://www.****.com");" System.out.println ("end execute task:" + id);} catch (Exception e) {e.printStackTrace ();}
Test case:
@ Testpublic void threadTest () throws InterruptedException {for (int iTuno; I < 35; iTunes +) {Thread t = new Thread (new CustomRunnable (I)); executor.execute (t);} Thread.sleep (1800000);}
Test results:
July 09, 2018 5:46:47 afternoon org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor initialize
Information: Initializing ExecutorService 'threadPoolTaskExecutor'
Begin execute threadPoolTaskExecutor-1-- task id: 0
Begin execute threadPoolTaskExecutor-2-- task id: 1
Begin execute threadPoolTaskExecutor-3-- task id: 2
Begin execute threadPoolTaskExecutor-4-- task id: 3
Begin execute threadPoolTaskExecutor-5-- task id: 4
End execute task: 4
Begin execute threadPoolTaskExecutor-5-- task id: 5
End execute task: 1
Begin execute threadPoolTaskExecutor-2-- task id: 6
End execute task: 0
Begin execute threadPoolTaskExecutor-1-- task id: 7
End execute task: 2
Begin execute threadPoolTaskExecutor-3-- task id: 8
End execute task: 3
Begin execute threadPoolTaskExecutor-4-- task id: 9
...
It can be found that corePoolSize-sized threads are created at the beginning of the thread pool, and then new tasks are put into BlockingQueue. The default is to use LinkedBlockingQueue, and the size is Integer.MAX_VALUE. Because the queue size is too large, the number of maxPoolSize-sized threads will not be created. Therefore, only when the thread has finished processing the current task will it deal with the next task. Therefore, the task that has just been added cannot be processed immediately.
Test scenario 2
You only need to open one line of queueCapacity, and the rest remains the same.
Test results:
July 09, 2018 6:07:13 afternoon org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor initialize
Information: Initializing ExecutorService 'threadPoolTaskExecutor'
Begin execute threadPoolTaskExecutor-1-- task id: 0
Begin execute threadPoolTaskExecutor-2-- task id: 1
Begin execute threadPoolTaskExecutor-3-- task id: 2
Begin execute threadPoolTaskExecutor-4-- task id: 3
Begin execute threadPoolTaskExecutor-5-- task id: 4
Begin execute threadPoolTaskExecutor-6-- task id: 15
Begin execute threadPoolTaskExecutor-7-- task id: 16
Begin execute threadPoolTaskExecutor-8-- task id: 17
Begin execute threadPoolTaskExecutor-9-- task id: 18
Begin execute threadPoolTaskExecutor-10-- task id: 19
Begin execute threadPoolTaskExecutor-11-- task id: 20
Begin execute threadPoolTaskExecutor-12-- task id: 21
Begin execute threadPoolTaskExecutor-14-- task id: 23
Begin execute threadPoolTaskExecutor-15-- task id: 24
Begin execute main-- task id: 26
Begin execute threadPoolTaskExecutor-13-- task id: 22
Begin execute threadPoolTaskExecutor-16-- task id: 25
Begin execute threadPoolTaskExecutor-11-- task id: 5
End execute task: 15
Begin execute threadPoolTaskExecutor-6-- task id: 6
End execute task: 23
Begin execute threadPoolTaskExecutor-14-- task id: 7
End execute task: 4
Begin execute threadPoolTaskExecutor-5-- task id: 8
End execute task: 17
Begin execute threadPoolTaskExecutor-8-- task id: 9
....
It can be found that because the number of initial tasks is larger than the corePoolSize size, thread pool initialization creates a purebred number of maxPoolSize size, and the subsequent new tasks will be added to the BlockingQueue queue, and then wait for the thread to finish processing a task before processing the tasks in the queue.
Conjecture
This may be due to the fact that queueCapacity is set to the default (Integer.MAX_VALUE), the number of initialized purebred corePoolSize is too small, and the thread processing speed is slow (business logic, network requests, etc.), resulting in subsequent tasks will always be added to the queue, delayed to be processed immediately.
Solution
Manually set the queueCapacity size, if the network request reason, you can set the timeout; if the business logic, find another way.
Thank you for your reading, the above is the content of "how to use ThreadPoolTaskExecutor", after the study of this article, I believe you have a deeper understanding of how to use ThreadPoolTaskExecutor, 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.