直接上代码
定时任务配置累,配置10个线程池
/**
* 定时任务县城池配置
* 可执行a,b任务并行,a,a任务串行
*/
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
}
}
测试代码
@Scheduled(cron = "${other.test1}")
private void test1() throws InterruptedException {
int a=1000;
for (int i=0 ;i<a;i++){
log.info("test1的:+ℹ️="+i);
Thread.sleep(1000L);
}
}
@Scheduled(cron = "${other.test2}")
private void test2() throws InterruptedException {
int a=1000;
for (int i=0 ;i<a;i++){
log.info("test2的:+ℹ️="+i);
Thread.sleep(1000L);
}
}
测试结果:

Java配置定时任务与多线程测试

该博客展示了如何在Java中配置定时任务,并使用10个线程池进行任务调度。测试代码中定义了两个定时任务test1和test2,通过cron表达式触发,每个任务内部包含循环,模拟耗时操作。测试结果显示任务可以并发执行。
1436

被折叠的 条评论
为什么被折叠?



