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