gpt4 book ai didi

java - ThreadPoolTask​​Scheduler 不适用于线程池

转载 作者:行者123 更新时间:2023-12-02 10:51:11 25 4
gpt4 key购买 nike

我创建

@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 - 结果是相同的

编辑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

最佳答案

如果您想在有任务正在运行的情况下执行该任务,则需要将其设为异步,例如:

@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");
}

关于java - ThreadPoolTask​​Scheduler 不适用于线程池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52169310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com