SpringBoot多线程环境下,解决多个定时器冲突问题
作者:瓦坎达forever
来源:blog.csdn.net/cssnnd/article/details/108328942
战术分析 :
我们的订单服务,一般会有一个待支付订单,而这个待支付订单是有时间限制的,比如阿里巴巴的订单是五天,淘宝订单是一天,拼多多订单是一天,美团订单是15分钟…
基金系统中,如何同时更新多个存储分区中的基金信息…

具体代码如下 :
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.scheduling.annotation.Async;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;import java.util.Date;public class SchedulerTaskController {private Logger logger= LoggerFactory.getLogger(SchedulerTaskController.class);private static final SimpleDateFormat dateFormat=new SimpleDateFormat("HH:mm:ss");private int count=0;(cron="*/6 * * * * ?")("threadPoolTaskExecutor")public void process(){logger.info("英文:this is scheduler task runing "+(count++));}(fixedRate = 6000)("threadPoolTaskExecutor")public void currentTime(){logger.info("中文:现在时间"+dateFormat.format(new Date()));}}
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;/**使用多线程的时候,往往需要创建Thread类,或者实现Runnable接口,如果要使用到线程池,我们还需要来创建Executors,* 在使用spring中,已经给我们做了很好的支持。只要要@EnableAsync就可以使用多线程* 通过spring给我们提供的ThreadPoolTaskExecutor就可以使用线程池。*///@Configuration 表示该类是一个配置类//所有的定时任务都放在一个线程池中,定时任务启动时使用不同都线程。public class TaskScheduleConfig {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 = "it-is-threaddemo-"; // 线程池名前缀("threadPoolTaskExecutor") // bean的名称,默认为首字母小写的方法名public ThreadPoolTaskExecutor getDemoThread(){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;}}
然后我们可以很清晰地看到

感谢您的阅读,也欢迎您发表关于这篇文章的任何建议,关注我,技术不迷茫!小编到你上高速。
正文结束
1.心态崩了!税前2万4,到手1万4,年终奖扣税方式1月1日起施行~

评论
