java – Spring 3为ThreadPoolTask​​Executor设置ThreadFactory

这是可能的还是由Application Server管理?将ThreadPoolTask​​Executor引用传递给bean是不费脑子的但是尝试在上述执行器上设置threadfactory似乎没有效果……

解决方法:

实际上,设置ThreadFactory也是一个明智的选择:

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="threadFactory" ref="threadFactory"/>
</bean>
<bean id="threadFactory" class="org.springframework.scheduling.concurrent.CustomizableThreadFactory">
    <constructor-arg value="Custom-prefix-"/>
</bean>

要么:

@Bean
public ThreadPoolTaskExecutor taskExecutor() {
    final ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setThreadFactory(threadFactory());
    return taskExecutor;
}

@Bean
public ThreadFactory threadFactory() {
    return new CustomizableThreadFactory("Custom-prefix-");
}

请注意,ThreadPoolTaskExecutorExecutorConfigurationSupport延伸,这是定义setThreadFactory(java.util.concurrent.ThreadFactory)的位置.

上一篇:java – 使用线程池和优先级队列的调度程序?


下一篇:java线程同步 – 这不应该工作,但它是:) –