[英]ThreadPoolTaskScheduler not work with pool of threads
我創造
@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 tims池將滿后,我將等待第一個空閑線程,並且該線程繼續工作。
但實際上我看到下一個:
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
結果相同
編輯2 :
@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
放在我的配置類中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.