@Scheduled和@Async解析与自定义线程池

仅仅使用@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")));
    }

}

总结

注解名称任务调度属性核心线程数
@ScheduledTaskSchedulingProperties1
@AsyncTaskExecutionProperties8
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值