我需要实现Multi Threaded后台进程。我的项目是spring,hibernate我试过
以下代码使用org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
以多线程方式执行以下后台操作。我需要知道为什么我的
线程数总是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>
答案 0 :(得分:0)
始终是一个,因为您只需向Thread
提交一个ThreadPoolTaskExecutor
。
Spring InitializingBean
(JavaDoc link)方法afterPropertiesSet()
仅在“应用程序”生命周期中调用一次,并且从我提供的示例中可以看出,是Thread
提交ThreadPoolTaskExecutor
的唯一内容。