栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springBoot的定时任务和多线程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springBoot的定时任务和多线程

springboot的定时任务

定时任务有三种,现在我学习的是springboot里面的一种,是比较简单的,下面是一些dome

package com.ljquan.test.dome.test;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class ScheduledService {
    
    @Scheduled(cron = "0/5 * * * * * ")
    public void test() {
        log.info("测试定时任务调度cron{}", System.currentTimeMillis());
    }
    @Scheduled(cron = "0/5 * * * * * ")
    public void test2() {
        log.info("测试定时任务调度2cron{}", 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注解开启对定时任务的支持,然后启动项目

package com.ljquan.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@EnableAsync
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
        System.out.println("(♥◠‿◠)ノ゙  测试Dome启动成功   ლ(´ڡ`ლ)゙"");
    }

}

输出结果:

(♥◠‿◠)ノ゙  测试Dome启动成功   ლ(´ڡ`ლ)゙"
2022-05-19 11:29:25.006  INFO 15868 --- [   scheduling-1] c.l.test.dome.test.ScheduledService      : 测试定时任务调度cron1652930965006
2022-05-19 11:29:25.007  INFO 15868 --- [   scheduling-1] c.l.test.dome.test.ScheduledService      : 测试定时任务调度2cron1652930965007
2022-05-19 11:29:27.843  INFO 15868 --- [   scheduling-1] c.l.test.dome.test.ScheduledService      : =====>>>>>使用fixedRate1652930967843
2022-05-19 11:29:27.843  INFO 15868 --- [   scheduling-1] c.l.test.dome.test.ScheduledService      : =====>>>>>fixedDelay1652930967843
2022-05-19 11:29:30.010  INFO 15868 --- [   scheduling-1] c.l.test.dome.test.ScheduledService      : 测试定时任务调度2cron1652930970010
2022-05-19 11:29:30.010  INFO 15868 --- [   scheduling-1] c.l.test.dome.test.ScheduledService      : 测试定时任务调度cron1652930970010
2022-05-19 11:29:32.836  INFO 15868 --- [   scheduling-1] c.l.test.dome.test.ScheduledService      : =====>>>>>使用fixedRate1652930972836
2022-05-19 11:29:32.852  INFO 15868 --- [   scheduling-1] c.l.test.dome.test.ScheduledService      : =====>>>>>fixedDelay1652930972852

可以看到,多个定时任务都是同一个线程的,如果一个定时任务出现问题,就会导致其他任务也会出现问题,所以就使用多线程的方式。

springboot的多线程 @Async 的使用、自定义Executor的配置方法

结合上面的代码

首先配置Executor,也就是重新配置我们的线程池执行器

package com.ljquan.test.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;


@Configuration
public class ExecutorConfig {
    
    private final int corePoolSize = 10;
    
    private final int maxPoolSize = 200;
    
    private final int queueCapacity = 10;

    
    @Bean
    public Executor Async1() {
        //线程池任务执行器  创建一个线程池
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //设置核心池大小
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        //设置队列容量
        executor.setQueueCapacity(queueCapacity);
        //设置线程前缀
        executor.setThreadNamePrefix("测试线程demo1-");
        executor.initialize();
        return executor;
    }

    
    @Bean
    public Executor Async2() {
        //线程池任务执行器  创建一个线程池
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //设置核心池大小
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        //设置队列容量
        executor.setQueueCapacity(queueCapacity);
        //设置线程前缀
        executor.setThreadNamePrefix("222测试线程demo2-");

        // rejection-policy:当pool已经达到max size的时候,如何处理新任务
        // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
        //设置拒绝执行处理程序
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

    //    类似的如果还有其他线程,也可以继续给其他线程配置对应的线程池
}

配置好了,就可以去使用了@Async(“Async1”)去对应的方法或者类里面去执行了,这样就相当于配置好了

 @Async("Async1")
    @Scheduled(cron = "0/5 * * * * * ")
    public void test() {
        log.info("测试定时任务调度cron{}", System.currentTimeMillis());
    }

类似于这样,就把当前的方法放进线程池里面执行了

执行结果:

(♥◠‿◠)ノ゙  测试Dome启动成功   ლ(´ڡ`ლ)゙"
2022-05-19 11:42:00.016  INFO 15908 --- [    测试线程demo1-2] c.l.test.dome.test.ScheduledService      : 测试定时任务调度2cron1652931720016
2022-05-19 11:42:00.016  INFO 15908 --- [    测试线程demo1-1] c.l.test.dome.test.ScheduledService      : 测试定时任务调度cron1652931720016
2022-05-19 11:42:00.228  INFO 15908 --- [   scheduling-1] c.l.test.dome.test.ScheduledService      : =====>>>>>使用fixedRate1652931720228
2022-05-19 11:42:00.244  INFO 15908 --- [   scheduling-1] c.l.test.dome.test.ScheduledService      : =====>>>>>fixedDelay1652931720244
2022-05-19 11:42:05.004  INFO 15908 --- [    测试线程demo1-3] c.l.test.dome.test.ScheduledService      : 测试定时任务调度cron1652931725004
2022-05-19 11:42:05.005  INFO 15908 --- [    测试线程demo1-4] c.l.test.dome.test.ScheduledService      : 测试定时任务调度2cron1652931725005
2022-05-19 11:42:05.240  INFO 15908 --- [   scheduling-1] c.l.test.dome.test.ScheduledService      : =====>>>>>使用fixedRate1652931725240

很明显的看到,这些线程都不是同一个线程了。关于springboot的多线程学习完成了。

gitee地址:
https://gitee.com/ljq4551/demo

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/946542.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号