Spring 2.5.6中的ThreadPooler

时间:2010-06-08 08:41:29

标签: java multithreading spring

我的应用程序使用Spring 2.5.6。我有一个服务,为某些特定任务创建显式线程。触发此服务调用通过石英时间调度程序发生。

问题:

在执行服务调用时,我想使用某种可以返回线程实例的线程池。我可以在Spring中使用任何实现吗?

3 个答案:

答案 0 :(得分:2)

我建议您使用java.util.concurrent.ExecutorService类。

您也可以使用Spring的ThreadPoolTaskExecutor课程。这两个类都可以配置为普通的Spring bean。

因为它只是普通的Spring bean,所以你可以对它进行编码。

如果升级到Spring 3,则可以获得更好的并发支持。例如,您可以使用@Async注释以声明方式在异步进程中执行方法。

我编写了一个关于如何在Spring 3 here中使用线程池的教程。

答案 1 :(得分:0)

您考虑过使用ThreadPoolTaskExecutor吗?您可以使用spring from the doco

轻松配置它
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="5" />
    <property name="maxPoolSize" value="10" />
    <property name="queueCapacity" value="25" />
</bean>

答案 2 :(得分:0)

但我有另一个问题...... 不知怎的,它不接受我设定的cofig ......

<bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="100" />
    <property name="maxPoolSize" value="200" />
    <property name="queueCapacity" value="100" />
    <property name="daemon" value="true" />
    <property name="keepAliveSeconds" value="54000" /> 
    <property name="allowCoreThreadTimeOut" value="true" />
    <property name="threadNamePrefix" value="ThreadPoolTaskExecutor For Cron" />
</bean> 

但不知何故,事情被设置为默认值..

游泳池大小:0 核心池大小:50 最大泳池大小:100 deamon:假的 活动计数:0 保持活力秒:60 thread prifix:taskExecutor -