仅仅使用@Scheduled
1.仅使用@Scheduled的时候其实是同一个线程在同步执行,使用到的是任务执行属性是org.springframework.boot.autoconfigure.task.TaskSchedulingProperties
2.源码参考
3.测试
使用@Scheduled和@Async
1.使用@Async是多线程异步调度,使用到的任务执行属性是org.springframework.boot.autoconfigure.task.TaskExecutionProperties
2.源码参考
线程默认前缀是task-;核心线程数是8
3.测试
所以如果大家一个类中定义了多个调度任务,建议方法加上@Async使用
自定义线程池
1.@Async使用ThreadPoolTaskExecutor类,默认的核心线程数量是8
2.自定义ThreadPoolTaskExecutor,指定核心线程数量
package com.young.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @Description: 线程池配置
* @Author: young
* @Date: 2022/8/9 11:24
*/
@Configuration
public class ExecutorConfig {
@Value("${executor.size.core:10}")
private Integer core;
@Value("${executor.size.max:20}")
private Integer max;
@Value("${executor.size.queue:100}")
private Integer queue;
@Value("${executor.keepalive.time:60}")
private Integer keepalive;
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//核心线程数
executor.setCorePoolSize(core);
//最大线程数
executor.setMaxPoolSize(max);
//队列中最大的数
executor.setQueueCapacity(queue);
//线程名称前缀
executor.setThreadNamePrefix("threadNamePrefix_");
//对拒绝task的处理策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//线程空闲后最大的存活时间(秒)
executor.setKeepAliveSeconds(keepalive);
//初始化加载
executor.initialize();
return executor;
}
}
3.测试
4.贴一下任务调度demo
package com.young.job;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @Description: job例子
* @Author: young
* cron表达式语法
* [秒] [分] [小时] [日] [月] [周] [年]
* @Date: 2022/8/8 15:00
*/
@EnableAsync
@EnableScheduling
@Component
public class JobDemo {
@Async
@Scheduled(cron = "0/5 * * * * *")
public void printLocalDate1() {
System.out.println("printLocalDate1:" + Thread.currentThread().getName() + "\t" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
@Async
@Scheduled(cron = "0/5 * * * * *")
public void printLocalDate2() {
System.out.println("printLocalDate2:" + Thread.currentThread().getName() + "\t" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
@Async
@Scheduled(cron = "0/5 * * * * *")
public void printLocalDate3() {
System.out.println("printLocalDate3:" + Thread.currentThread().getName() + "\t" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
}
总结
注解名称 | 任务调度属性 | 核心线程数 |
---|---|---|
@Scheduled | TaskSchedulingProperties | 1 |
@Async | TaskExecutionProperties | 8 |