First, threadpooltaskexecutor configuration
<bean id = "Taskexecutor" class= " Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor " > <!-- thread pool maintains a minimum number of threads --> <property name = "Corepoolsize" value = "5" /> <!-- thread pool maintain idle time allowed by thread --> <property name = "Keepaliveseconds" value = " /> <!" -- thread pool maintains the maximum number of threads --> <property name = "Maxpoolsize" value = /> <!-- buffer queue used by the thread pool --> < property name = "queuecapacity" value = " /> </bean>";
Attribute Field Description:
Corepoolsize: Thread pool maintains a minimum number of threads
Keepaliveseconds thread pool to maintain idle time allowed by threads
Maxpoolsize thread pool maintains the maximum number of threads
Buffer queue used by the queuecapacity thread pool
Ii. Execute procedure of the Execute (Runnable) method
If the number of threads in the thread pool is less than corepoolsize at this point, even if the threads in the thread pool are idle, a new thread is created to handle the task being added.
If the number in the thread pool is equal to corepoolsize, but the buffer queue Workqueue is not full, then the task is placed in the buffer queue.
If the number of threads in the thread pool is greater than corepoolsize, the buffer queue Workqueue full, and the number of thread pools is less than maximumpoolsize, a new thread is built to handle the task being added.
If the number of threads in the thread pool is greater than corepoolsize, the buffer queue is workqueue full, and the number in the thread pool equals maximumpoolsize, the task is handled by handler the policy specified. That is: the priority of the processing task is: Core thread corepoolsize, Task queue workqueue, maximum thread maximumpoolsize, if all three are full, use handler to handle the rejected task.
When the number of threads in the thread pool is greater than corepoolsize, if a thread is idle for more than KeepAliveTime, the thread is terminated. This allows the thread pool to dynamically adjust the number of threads in the pool.
Three, sample code
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Applicationcontext.xml"); Threadpooltaskexecutor pooltaskexecutor = (threadpooltaskexecutor) ctx.getbean ("Taskexecutor"); Thread udpthread = new Thread (UDP);p ooltaskexecutor.execute (udpthread); Gets the number of threads active for the current thread pool: int count = Pooltaskexecutor.getactivecount (); Logger.debug ("[x]-now ThreadPool active threads totalnum:" +count);
This article is from the "Ciyo Technology sharing" blog, please be sure to keep this source http://ciyorecord.blog.51cto.com/6010867/1941673
Spring Thread pool Threadpooltaskexecutor