我创建
@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
-结果相同
EDIT2 :
@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
答案 0 :(得分:1)
即使您正在执行一项任务,也要使其执行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");
}
答案 1 :(得分:0)
感谢提示@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
放在我的配置类中