Spring线程池配置

来源:互联网 发布:加工中心手动编程平面 编辑:程序博客网 时间:2024/05/19 07:11

原文:http://blog.csdn.net/shimiso/article/details/8964527


spring配置

[html] view plain copy
  1. <!-- 异步线程池 -->  
  2. <bean id="taskExecutor"  
  3.     class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">  
  4.     <!-- 核心线程数 -->  
  5.     <property name="corePoolSize" value="10" />  
  6.     <!-- 最大线程数 -->  
  7.     <property name="maxPoolSize" value="100" />  
  8.     <!-- 队列最大长度 >=mainExecutor.maxSize -->  
  9.     <property name="queueCapacity" value="1000" />  
  10.     <!-- 线程池维护线程所允许的空闲时间 -->  
  11.     <property name="keepAliveSeconds" value="300" />  
  12.     <!-- 线程池对拒绝任务(无线程可用)的处理策略 -->  
  13.     <property name="rejectedExecutionHandler">  
  14.         <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />  
  15.     </property>  
  16. </bean>  

Java代码

[java] view plain copy
  1. import org.springframework.beans.factory.annotation.Autowired;  
  2. import org.springframework.core.task.TaskExecutor;  
  3.   
  4. public class ThreadPoolTest {  
  5.     @Autowired  
  6.     private TaskExecutor taskExecutor;// 线程池  
  7.   
  8.     // 将创建的线程添加到线程池中  
  9.     public void test() throws Exception {  
  10.         for (int i = 0; i < 10; i++) {  
  11.             this.taskExecutor.execute(new AppContentDataPushThread());  
  12.         }  
  13.     }  
  14.   
  15.     class AppContentDataPushThread implements Runnable {  
  16.   
  17.         public AppContentDataPushThread() {  
  18.         }  
  19.   
  20.         @Override  
  21.         public void run() {  
  22.             System.out.println("执行线程");  
  23.         }  
  24.     }  

原创粉丝点击