Thread pool Threadpooltaskexecutor in spring

Source: Internet
Author: User

1. Call the Threadpooltaskexecutor in the spring frame directly

Threadpooltaskexecutor pooltaskexecutor = new Threadpooltaskexecutor ();// The buffer queue used by the thread pool pooltaskexecutor.setqueuecapacity (200);//The thread pool maintains a minimum number of threads pooltaskexecutor.setcorepoolsize (5); The maximum number of threads maintained by the thread pool pooltaskexecutor.setmaxpoolsize (1000);//thread pool maintenance threads allow idle time pooltaskexecutor.setkeepaliveseconds (30000) ;p ooltaskexecutor.initialize ();


2, in the Spring.xml configuration

<!--Configuring the thread pool--><bean id = "Taskexecutor" class = " Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor "><!--thread pool maintains a minimum number of threads--><span style=" White-space:pre "></span><property name =" Corepoolsize "value =" 5 "/><!--thread pool to maintain idle time allowed by threads-->< Span style= "White-space:pre" ></span><property name = "Keepaliveseconds" value = "30000"/><!-- Maximum number of threads maintained by thread pool--><span style= "White-space:pre" ></span><property name = "Maxpoolsize" value = "1000"/ >< buffer queue used by the!--thread pool--><span style= "White-space:pre" ></span><property name = "Queuecapacity" Value = "/></bean>"

Inside the program gets:

ApplicationContext CTX = new Classpathxmlapplicationcontext ("Applicationcontext.xml"); Threadpooltaskexecutor pooltaskexecutor = (threadpooltaskexecutor) ctx.getbean ("Taskexecutor");

To start a thread with a thread pool:

Pooltaskexecutor.execute (New Xxxthread ());


3. Configuration explanation

When a task is added to the thread pool by the Execute (Runnable) method:

1. If the number in the thread pool is less than corepoolsize at this point, even if the threads in the thread pool are idle, create a new thread 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, the thread will be terminated if a thread has more than KeepAliveTime idle time. This allows the thread pool to dynamically adjust the number of threads in the pool.


Thread pool Threadpooltaskexecutor in spring

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.