[Thread pool] Realize multi-threaded concurrent timing tasks

1. Why do you need to configure multi-threaded scheduled tasks

The method annotated by @Scheduled in springboot is a scheduled execution task. By default, it is single-threaded. Even multiple scheduled tasks run in the same single thread (scheduled-1). If one of the scheduled tasks If blocking occurs, all other scheduled task threads in the project will not be executed. The consequences are very serious, so it is necessary to configure multi-threaded timing tasks.

Two, single thread timing task

导入依赖

<dependencies>

 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
 </dependency>

 <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
 </dependency>

 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
 </dependency>

</dependencies>

创建定时任务

@Slf4j
@Component
public class ScheduledService {
    
    

	/**
	 * fixedRate:定义一个按一定频率执行的定时任务
	 * fixedDelay:定义该任务延迟执行时间。
	 * cron:通过表达式来配置任务执行时间
	*/
   @Scheduled(cron = "0/5 * * * * *")
   public void scheduled(){
    
    
       log.info("=====>>>>>使用cron  {}",System.currentTimeMillis());
   }

   @Scheduled(fixedRate = 5000)
   public void scheduled1() {
    
    
       log.info("=====>>>>>使用fixedRate{}", System.currentTimeMillis());
   }

   @Scheduled(fixedDelay = 5000)
   public void scheduled2() {
    
    
       log.info("=====>>>>>fixedDelay{}",System.currentTimeMillis());
   }
   
}

在主类上使用@EnableScheduling注解开启对定时任务的支持,然后启动项目

@SpringBootApplication
@EnableScheduling
public class WebApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(WebApplication.class,args);
    }
}

It can be seen that the three scheduled tasks have been executed, and they are executed serially in the same thread. If there is only one scheduled task, it is definitely no problem to do so. When the number of scheduled tasks increases, if one task is stuck, it will cause other tasks to fail. implement.
insert image description here

3. Multi-thread timing task

In the SpringBoot project, the config configuration class is generally used to add configuration, so create a new AsyncConfig class.

@Configuration
@EnableAsync
public class AsyncConfig {
    
    

    /*
     *此处成员变量应该使用@Value从配置中读取
    */
   private int corePoolSize = 10;
   private int maxPoolSize = 200;
   private int queueCapacity = 10;

   @Bean
   public Executor taskExecutor() {
    
    
       ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
       executor.setCorePoolSize(corePoolSize);
       executor.setMaxPoolSize(maxPoolSize);
       executor.setQueueCapacity(queueCapacity);
       executor.initialize();
       return executor;
   }

}

Then add @Async to the class or method of the scheduled task

@Slf4j
@Component
public class ScheduledService {
    
    

	/**
	 * fixedRate:定义一个按一定频率执行的定时任务
	 * fixedDelay:定义该任务延迟执行时间。
	 * cron:通过表达式来配置任务执行时间
	*/
   @Async
   @Scheduled(cron = "0/5 * * * * *")
   public void scheduled(){
    
    
       log.info("=====>>>>>使用cron  {}",System.currentTimeMillis());
   }
   
   @Async
   @Scheduled(fixedRate = 5000)
   public void scheduled1() {
    
    
       log.info("=====>>>>>使用fixedRate{}", System.currentTimeMillis());
   }
   
   @Async
   @Scheduled(fixedDelay = 5000)
   public void scheduled2() {
    
    
       log.info("=====>>>>>fixedDelay{}",System.currentTimeMillis());
   }
   
}

insert image description here

Guess you like

Origin blog.csdn.net/m0_46638350/article/details/130926688