使用Spring ThreadPoolTask​​Executor,线程计数始终为1

时间:2014-06-05 01:32:09

标签: java multithreading spring

我需要实现Multi Threaded后台进程。我的项目是spring,hibernate我试过

以下代码使用org.springframework.scheduling.concurrent.ThreadPoolTask​​Executor

以多线程方式执行以下后台操作。我需要知道为什么我的

线程数总是1?

public class UserUpdateProcessor implements InitializingBean {
private ThreadPoolTaskExecutor executor;

    public void afterPropertiesSet() throws Exception {  
      for(int i = 0; i < 10)    //added this like after  the 1st reply     
        executor.execute(new UserBackgorundRunner ());
    }
 }

  private class UserBackgorundRunner extends Thread {

    public UserBackgorundRunner() {
       this.setDaemon(true);
       this.setPriority(MIN_PRIORITY);
    }
    public void run() {
       List<User> users = getUserList();;

     for (User user : users) {
    try {
   log.debug("Active count :::::::::::::::::::::::::::::"+executor.getActiveCount());
        upgradeUserInBackground(user);
    } catch (Exception e) {
        LOGGER.warn("Fail to upgrade user");
    }
}

}

 My spring.xml looks like 
<bean id="userThreadPool"   
 class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">      
  <property name="corePoolSize"><value>10</value></property>
  <property name="maxPoolSize"><value>15</value></property>
  <property name="queueCapacity"><value>50</value></property>
 </bean>


<bean id="userProcessor" class="com.user.UserUpdateProcessor"
autowire="byType">
<property name="executor" ref="userThreadPool" />
 </bean>

1 个答案:

答案 0 :(得分:0)

始终是一个,因为您只需向Thread提交一个ThreadPoolTaskExecutor

Spring InitializingBeanJavaDoc link)方法afterPropertiesSet()仅在“应用程序”生命周期中调用一次,并且从我提供的示例中可以看出,是Thread提交ThreadPoolTaskExecutor的唯一内容。