文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

SpringBoot定时任务多线程实现示例

2024-04-02 19:55

关注

测试Spring Boot定时任务冲突时,使用的线程数量

引入依赖:

Spring Boot 2.6.1


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

简单的测试类


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


@Component
@Slf4j
public class Task {

    @Scheduled(cron="*/2 * * * * ?")
    public void process(){
        log.info("do something");
    }

    @Scheduled(fixedRate = 2000)
    public void currentTime(){
        log.info("做点什么");
    }
}

启动类开启定时任务:@EnableScheduling

测试结果如下:

在这里插入图片描述

由结果可见,main线程启动以后和scheduling-1线程跑了两个任务

由此可见,Spring Boot 定时器 默认是 单线程

我们新增两个定时任务来看看这样会有什么后果


@Scheduled(cron="*/2 * * * * ?")
public void process2() throws InterruptedException {
     Thread.sleep(5000L);
     log.info("do something use long time");
 }
 @Scheduled(cron="*/2 * * * * ?")
 public void process3() throws InterruptedException {
     Thread.sleep(10000L);
     log.info("do something use long long time");
 }

2021-12-22 16:30:28.735  INFO 14520 --- [           main] c.e.s.SpringBootScheduledApplication     : Starting SpringBootScheduledApplication using Java 1.8.0_202 on TCZ-D with PID 14520 (D:\code\SpringBootScheduled\target\classes started by Administrator in D:\code\SpringBootScheduled)
2021-12-22 16:30:28.738  INFO 14520 --- [           main] c.e.s.SpringBootScheduledApplication     : No active profile set, falling back to default profiles: default
2021-12-22 16:30:29.315  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:30:29.318  INFO 14520 --- [           main] c.e.s.SpringBootScheduledApplication     : Started SpringBootScheduledApplication in 0.937 seconds (JVM running for 3.068)
2021-12-22 16:30:30.002  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : do something
2021-12-22 16:30:40.003  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : do something use long long time
2021-12-22 16:30:45.005  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : do something use long time
2021-12-22 16:30:45.005  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:30:45.005  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : do something
2021-12-22 16:30:45.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:30:45.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:30:45.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:30:45.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:30:45.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:30:55.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : do something use long long time
2021-12-22 16:30:55.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:30:55.006  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:30:55.007  INFO 14520 --- [   scheduling-1] c.e.springbootscheduled.scheduled.Task   : do something

很明显任务会发生错乱,严重时会导致线程阻塞,最后崩溃

解决方法:引入线程池

新增配置类,配置线程池


package com.example.springbootscheduled.config;

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

import java.util.concurrent.ThreadPoolExecutor;


@Configuration
@EnableAsync
public class TaskConfig {

    
    private static final int corePoolSize = 10;
    
    private static final int maxPoolSize = 100;
    
    private static final int keepAliveTime = 10;
    
    private static final int queueCapacity = 200;
    
    private static final String threadNamePrefix = "task-thread-";

    
    @Bean("taskExecutor")
    public ThreadPoolTaskExecutor getThread() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(keepAliveTime);
        executor.setKeepAliveSeconds(queueCapacity);
        executor.setThreadNamePrefix(threadNamePrefix);
        //线程池拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}

每个定时任务添加注解 @Async("taskExecutor")


@Async("taskExecutor")
@Scheduled(fixedRate = 2000)
public void currentTime(){
    log.info("做点什么");
}

启动测试


2021-12-22 16:41:36.141  INFO 19424 --- [           main] c.e.s.SpringBootScheduledApplication     : Starting SpringBootScheduledApplication using Java 1.8.0_202 on TCZ-D with PID 19424 (D:\code\SpringBootScheduled\target\classes started by Administrator in D:\code\SpringBootScheduled)
2021-12-22 16:41:36.143  INFO 19424 --- [           main] c.e.s.SpringBootScheduledApplication     : No active profile set, falling back to default profiles: default
2021-12-22 16:41:36.991  INFO 19424 --- [           main] c.e.s.SpringBootScheduledApplication     : Started SpringBootScheduledApplication in 1.325 seconds (JVM running for 3.392)
2021-12-22 16:41:37.008  INFO 19424 --- [  task-thread-1] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:41:38.001  INFO 19424 --- [  task-thread-2] c.e.springbootscheduled.scheduled.Task   : do something
2021-12-22 16:41:38.991  INFO 19424 --- [  task-thread-5] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:41:40.003  INFO 19424 --- [  task-thread-8] c.e.springbootscheduled.scheduled.Task   : do something
2021-12-22 16:41:40.991  INFO 19424 --- [  task-thread-9] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:41:42.001  INFO 19424 --- [ task-thread-10] c.e.springbootscheduled.scheduled.Task   : do something
2021-12-22 16:41:42.989  INFO 19424 --- [  task-thread-5] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:41:43.002  INFO 19424 --- [  task-thread-3] c.e.springbootscheduled.scheduled.Task   : do something use long time
2021-12-22 16:41:44.002  INFO 19424 --- [  task-thread-9] c.e.springbootscheduled.scheduled.Task   : do something
2021-12-22 16:41:44.990  INFO 19424 --- [  task-thread-5] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:41:45.002  INFO 19424 --- [  task-thread-6] c.e.springbootscheduled.scheduled.Task   : do something use long time
2021-12-22 16:41:46.001  INFO 19424 --- [  task-thread-9] c.e.springbootscheduled.scheduled.Task   : do something
2021-12-22 16:41:46.990  INFO 19424 --- [  task-thread-6] c.e.springbootscheduled.scheduled.Task   : 做点什么
2021-12-22 16:41:47.002  INFO 19424 --- [  task-thread-1] c.e.springbootscheduled.scheduled.Task   : do something use long time
2021-12-22 16:41:48.002  INFO 19424 --- [  task-thread-4] c.e.springbootscheduled.scheduled.Task   : do something use long long time

到此这篇关于SpringBoot定时任务多线程实现示例的文章就介绍到这了,更多相关SpringBoot定时任务多线程内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯