Spring Thread pool Threadpooltaskexecutor

Source: Internet
Author: User

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
    1. 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.

    2. 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.

    3. 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.

    4. 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.

    5. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.