• SpringBoot多线程环境下,解决多个定时器冲突问题(荣耀典藏版)


    战术分析

    实际开发项目中一定不止一个定时器,很多场景都需要用到,而多个定时器带来的问题 : 就是如何避免多个定时器的互相冲突;

     

    使用场景 

    我们的订单服务,一般会有一个待支付订单,而这个待支付订单是有时间限制的,比如阿里巴巴的订单是五天,淘宝订单是一天,拼多多订单是一天,美团订单是15分钟…

    基金系统中,如何同时更新多个存储分区中的基金信息…(点击下载2021年最新阿里p7面试题教程) 

    总的来说,实际开发中定时器需要解决多个定时器同时并发的问题,也要解决定时器之间的冲突问题

    问题不大,说到并发那就离不开多线程了…慢慢看看就懂了

    问题场景重现 

     

     我们清晰的看到执行结果都是scheduling-1

    就此可以判定,Springboot定时器默认的是单线程的

    但是问题就来了,如果在线程争夺资源后,某个线程需要比较长时间才能执行完,那其他的定时器怎么办,都只能进入等待状态,时间越久,累计等待的定时器越多,这就容易引起雪崩…

    其实只需要添加一个配置类然后加注解就可以解决问题了

    添加注解

     具体代码如下 :

    1. import org.slf4j.Logger;
    2. import org.slf4j.LoggerFactory;
    3. import org.springframework.scheduling.annotation.Async;
    4. import org.springframework.scheduling.annotation.Scheduled;
    5. import org.springframework.stereotype.Component;
    6. import java.text.SimpleDateFormat;
    7. import java.util.Date;
    8. @Component
    9. public class SchedulerTaskController {
    10.     private Logger logger= LoggerFactory.getLogger(SchedulerTaskController.class);
    11.     private static final SimpleDateFormat dateFormat=new SimpleDateFormat("HH:mm:ss");
    12.     private int count=0;
    13.     @Scheduled(cron="*/6 * * * * ?")
    14.     @Async("threadPoolTaskExecutor")
    15.     public void process(){
    16.         logger.info("英文:this is scheduler task runing "+(count++));
    17.     }
    18.     @Scheduled(fixedRate = 6000)
    19.     @Async("threadPoolTaskExecutor")
    20.     public void currentTime(){
    21.         logger.info("中文:现在时间"+dateFormat.format(new Date()));
    22.     }
    23. }

     配置类如下 :

     

    具体代码如下 :

    1. import org.springframework.context.annotation.Bean;
    2. import org.springframework.context.annotation.Configuration;
    3. import org.springframework.scheduling.annotation.EnableAsync;
    4. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
    5. import java.util.concurrent.ThreadPoolExecutor;
    6. /**使用多线程的时候,往往需要创建Thread类,或者实现Runnable接口,如果要使用到线程池,我们还需要来创建Executors,
    7.  * 在使用spring中,已经给我们做了很好的支持。只要要@EnableAsync就可以使用多线程
    8.  * 通过spring给我们提供的ThreadPoolTaskExecutor就可以使用线程池。*/
    9. //@Configuration 表示该类是一个配置类
    10. @Configuration
    11. @EnableAsync
    12. //所有的定时任务都放在一个线程池中,定时任务启动时使用不同都线程。
    13. public class TaskScheduleConfig {
    14.     private static final int corePoolSize = 10;         // 默认线程数
    15.     private static final int maxPoolSize = 100;       // 最大线程数
    16.     private static final int keepAliveTime = 10;   // 允许线程空闲时间(单位:默认为秒),十秒后就把线程关闭
    17.     private static final int queueCapacity = 200;   // 缓冲队列数
    18.     private static final String threadNamePrefix = "it-is-threaddemo-"// 线程池名前缀
    19.     @Bean("threadPoolTaskExecutor") // bean的名称,默认为首字母小写的方法名
    20.     public ThreadPoolTaskExecutor getDemoThread(){
    21.         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    22.         executor.setCorePoolSize(corePoolSize);
    23.         executor.setMaxPoolSize(maxPoolSize);
    24.         executor.setQueueCapacity(keepAliveTime);
    25.         executor.setKeepAliveSeconds(queueCapacity);
    26.         executor.setThreadNamePrefix(threadNamePrefix);
    27.         //线程池拒绝任务的处理策略
    28.         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    29.         //初始化
    30.         executor.initialize();
    31.         return executor;
    32.     }
    33. }

    然后我们可以很清晰地看到

     

     只有当你开始,你才会到达你的理想和目的地,只有当你努力,
    你才会获得辉煌的成功,只有当你播种,你才会有所收获。只有追求,
    才能尝到成功的味道,坚持在昨天叫立足,坚持在今天叫进取,坚持在明天叫成功。欢迎所有小伙伴们点赞+收藏!!!

     

  • 相关阅读:
    为什么说阿里云服务器5M带宽是最划算的?
    contentEditable属性
    【21天打卡】前端攻城狮重学算法之-顺序查找
    centos下安装mysql8版本
    第1关:节点监听机制
    异步编程 - 03 线程池ThreadPoolExecutor原理剖析&源码详解
    C++中的fsanitize指令
    【Linux】第六章 进程地址空间(程序在内存中存储+虚拟地址+页表+mm_struct+写实拷贝+解释fork返回值)
    Rust中derive宏的作用及常用trait
    docker安装wiki
  • 原文地址:https://blog.csdn.net/weixin_48321993/article/details/125907946