ThreadPoolTask Scheduler不适用于线程池
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ThreadPoolTask Scheduler不适用于线程池相关的知识,希望对你有一定的参考价值。
我创造
@Bean
ThreadPoolTaskScheduler taskScheduler(){
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(5);
threadPoolTaskScheduler.setAwaitTerminationSeconds(1);
threadPoolTaskScheduler.setThreadNamePrefix("Test-");
threadPoolTaskScheduler.initialize();
return threadPoolTaskScheduler;
}
在我的组件中我使用它:
@PostConstruct
public void test() {
taskScheduler.scheduleWithFixedDelay(() -> {
try {
Thread.sleep(9000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("test");
}, 1000L);
}
我等待每1秒钟从PoolSize(5)
开始一个线程,并且在5个时间后池将满,我将等待第一个免费线程并且它继续工作。
但实际上我看到下一个:
2018-09-04 18:06:42.769 INFO 10128 --- [main] c.e.scheduling.SchedulingApplication : Started SchedulingApplication in 1.69 seconds (JVM running for 2.193)
2018-09-04 18:06:51.385 INFO 10128 --- [Test-1] com.example.scheduling.MyScheduler : test
2018-09-04 18:07:01.387 INFO 10128 --- [Test-1] com.example.scheduling.MyScheduler : test
2018-09-04 18:07:11.389 INFO 10128 --- [Test-2] com.example.scheduling.MyScheduler : test
每9秒进行一次线程打印测试
编辑:
我测试过了
scheduleAtFixedRate
- 结果是一样的
Aaditi:
@PostConstruct
public void test() {
taskScheduler.scheduleAtFixedRate(this::test2, 1000L);
}
@Async
public void test2() {
try {
Thread.sleep(9000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("test");
}
@EnableAsync
@EnableScheduling
@Configuration
public class JavaConfig {
没有帮助:
2018-09-05 10:31:40.160 INFO 13636 --- [ Test-3] com.example.scheduling.MyScheduler : test
2018-09-05 10:31:49.160 INFO 13636 --- [ Test-3] com.example.scheduling.MyScheduler : test
2018-09-05 10:31:58.160 INFO 13636 --- [ Test-3] com.example.scheduling.MyScheduler : test
2018-09-05 10:32:07.160 INFO 13636 --- [ Test-3] com.example.scheduling.MyScheduler : test
2018-09-05 10:32:16.160 INFO 13636 --- [ Test-3] com.example.scheduling.MyScheduler : test
2018-09-05 10:32:25.160 INFO 13636 --- [ Test-3] com.example.scheduling.MyScheduler : test
2018-09-05 10:32:34.160 INFO 13636 --- [ Test-3] com.example.scheduling.MyScheduler : test
2018-09-05 10:32:43.160 INFO 13636 --- [ Test-3] com.example.scheduling.MyScheduler : test
2018-09-05 10:32:52.160 INFO 13636 --- [ Test-3] com.example.scheduling.MyScheduler : test
2018-09-05 10:33:01.160 INFO 13636 --- [ Test-3] com.example.scheduling.MyScheduler : test
2018-09-05 10:33:10.160 INFO 13636 --- [ Test-3] com.example.scheduling.MyScheduler : test
2018-09-05 10:33:19.160 INFO 13636 --- [ Test-3] com.example.scheduling.MyScheduler : test
答案
如果你想要执行任务,你需要使它成为Async
,即使有一个正在运行,例如:
@PostConstruct
public void test() {
taskScheduler.scheduleAtFixedRate(this::makeLog, 1000);
}
@Async
public void makeLog() {
try {
Thread.sleep(9000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("test");
}
另一答案
感谢提示@Sun我找到了解决方案:
@PostConstruct
public void test() {
taskScheduler.scheduleAtFixedRate(testBean::test, 1000L);
}
并将方法移动到另一个类,因为我默认使用proxy
:
@Slf4j
@Component
public class TestBean {
@Async
public void test(){
try {
Thread.sleep(9000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("hz");
}
}
并将@EnableAsync
放在我的配置类上
以上是关于ThreadPoolTask Scheduler不适用于线程池的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 ThreadPoolTaskExecutor 为任务设置超时
Spring ThreadPoolTask Executor自动装配不同的实例
如何在 Spring Boot 中创建不同的 ThreadPoolTaskExecutor? [复制]
OpenShift 中的 Spring 批处理 JDBCPagingItemReader、ThreadPoolTaskExecutor 和多个 pod