@Configuration
@EnableScheduling
@Slf4j
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
// taskRegistrar.setScheduler(scheduledTaskExecutor());
taskRegistrar.setScheduler(Executors.newScheduledThreadPool(5));
}
}
通过同时测试几个任务发现,所有的任务都是在同一个线程池中的同一个线程来完成的,定时器Scheduled的测试用例比较难写,所以就写了了个下面的做UT吧
@Configuration
@EnableScheduling
@Slf4j
@ContextConfiguration(initializers = ConfigDataApplicationContextInitializer.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringTaskScheduleTest.class)
public class ConcurrentScheduleTest {
private static AtomicInteger getCountCron1 = new AtomicInteger(0);
private static AtomicInteger getCountCron2 = new AtomicInteger(0);
private static AtomicInteger getCountCron3 =