· AsyncConfig

 1 package com.miaoshaProject.async;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.scheduling.annotation.Async;
 6 import org.springframework.scheduling.annotation.EnableAsync;
 7 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 8 
 9 import java.util.concurrent.Executor;
10 
11 /**
12  * @Author wangshuo
13  * @Date 2022/5/4, 18:39
14  * 异步配置类
15  */
16 @Configuration
17 @EnableAsync
18 public class AsyncConfig {
19 
20     /*
21      *此处成员变量应该使用@Value从配置中读取
22      */
23     private int corePoolSize = 10;
24     private int maxPoolSize = 200;
25     private int queueCapacity = 10;
26 
27     @Bean
28     public Executor taskExecutor() {
29         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
30         executor.setCorePoolSize(corePoolSize);
31         executor.setMaxPoolSize(maxPoolSize);
32         executor.setQueueCapacity(queueCapacity);
33         executor.initialize();
34         return executor;
35     }
36 
37 }

· 定时器服务

 1 package com.miaoshaProject.async;
 2 
 3 import lombok.extern.slf4j.Slf4j;
 4 import org.springframework.scheduling.annotation.Async;
 5 import org.springframework.scheduling.annotation.Scheduled;
 6 import org.springframework.stereotype.Component;
 7 
 8 /**
 9  * @Author wangshuo
10  * @Date 2022/5/4, 18:37
11  * 定时器服务
12  */
13 @Slf4j
14 @Component
15 @Async
16 public class ScheduledService {
17 
18     //@Scheduled(cron = "0/5 * * * * *")
19     public void scheduled(){
20         //log.info("=====>>>>>使用cron  {}",System.currentTimeMillis());
21     }
22 
23     //@Scheduled(fixedRate = 5000)
24     public void scheduled1() {
25         //log.info("=====>>>>>使用fixedRate{}", System.currentTimeMillis());
26     }
27 
28     //@Scheduled(fixedDelay = 5000)
29     public void scheduled2() {
30         //log.info("=====>>>>>fixedDelay{}",System.currentTimeMillis());
31     }
32 
33 }

· pom.xml

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
            <scope>provided</scope>
        </dependency>