Spring线程池配置

原文: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.       
  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.     }  

你可能感兴趣的:(Spring)