这篇文章主要介绍了弹簧引导如何通过@Scheduled实现定时任务及多线程配置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
使用@Scheduled可以很容易实现定时任务
弹簧引导的版本2.1.6.RELEASE
包com.abc.demo.common; 进口org.slf4j.Logger; 进口org.slf4j.LoggerFactory; 进口org.springframework.scheduling.annotation.EnableScheduling; 进口org.springframework.scheduling.annotation.Scheduled; 进口org.springframework.stereotype.Component; 进口java.text.SimpleDateFormat; 进口java.util.concurrent.TimeUnit; @EnableScheduling @ component 公开课ScheduleSetting { 私人最后日志记录器=LoggerFactory.getLogger (Tasks.class); @Scheduled (fixedRate=10000, initialDelay=2000) 公共空间scheduleRead () { 尝试{ 长时间戳=System.currentTimeMillis (); SimpleDateFormat SimpleDateFormat=new SimpleDateFormat (“yyyy-MM-dd HH: mm: ss”); 线程的线程=Thread.currentThread (); system . out。println (“cron1任务开始,开始=" + simpleDateFormat.format(时间戳)+”,threadId=" + thread.getId () +”, threadName=" + thread.getName ()); 长endStamp=System.currentTimeMillis (); 尝试{ TimeUnit.SECONDS.sleep (20); }捕捉(InterruptedException e) { e.printStackTrace (); } system . out。println (“cron1任务正在运行的线程名称:“+ thread.getName() +“结束,开始=" + simpleDateFormat.format(时间戳)+”,结束=" + simpleDateFormat.format (endStamp)); System.out.println (“+ + + + + + + + + + + + + + + + + + + + + + + +”); }捕捉(异常e) { logger.error (e.getMessage ()); } } @Scheduled (fixedRate=5000, initialDelay=1000) 公共空间scheduleConvert () { 尝试{ 长时间戳=System.currentTimeMillis (); SimpleDateFormat SimpleDateFormat=new SimpleDateFormat (“yyyy-MM-dd HH: mm: ss”); 线程的线程=Thread.currentThread (); system . out。println (“cron2任务开始,开始=" + simpleDateFormat.format(时间戳)+”,threadId=" + thread.getId () +”, threadName=" + thread.getName ()); 尝试{ TimeUnit.SECONDS.sleep (10); }捕捉(InterruptedException e) { e.printStackTrace (); } 长endStamp=System.currentTimeMillis (); system . out。println (“cron2任务正在运行的线程名称:“+ thread.getName() +“结束,开始=" + simpleDateFormat.format(时间戳)+”,结束=" + simpleDateFormat.format (endStamp)); System.out.println (“===================="); }捕捉(异常e) { logger.error (e.getMessage ()); } } }
运行输出内容为
cron2任务开始,开始=2019-10-11 17:31:52 threadId=34, threadName=安排一 cron2任务正在运行的线程名称:安排一结束,开始=2019-10-11 17:31:52结束=2019-10-11 17:32:02====================cron1任务开始,开始=2019-10-11 17:32:02 threadId=34, threadName=安排一 cron1任务正在运行的线程名称:安排一结束,开始=2019-10-11 17:32:02结束=2019-10-11 17:32:02 ++++++++++++++++++++++++ cron2任务开始,开始=2019-10-11 17:32:22 threadId=34, threadName=安排一 cron2任务正在运行的线程名称:安排一结束,开始=2019-10-11 17:32:22结束=2019-10-11 17:32:32…… 注:
cron2执行完后才会执行cron1
原因:
春默认是以单线程执行任务调度
春季的定时任务默认最大运行线程数为1,多个任务执行起来时间会有问题
1。配置线程池
在配置文件application.properties中添加
#线程池大小 spring.task.scheduling.pool.size=5 #线程名前缀 spring.task.scheduling.thread-name-prefix=myScheduling -
输出内容变为
cron2任务开始,开始=2019-10-11 17:34:48 threadId=34, threadName=myScheduling-1 cron1任务开始,开始=2019-10-11 17:34:49 threadId=35, threadName=myScheduling-2 cron2任务正在运行的线程名称:myScheduling-1结束,开始=2019-10-11 17:34:48结束=2019-10-11 17:34:58====================cron2任务开始,开始=2019-10-11 17:34:58 threadId=34, threadName=myScheduling-1 cron2任务正在运行的线程名称:myScheduling-1结束,开始=2019-10-11 17:34:58结束=2019-10-11 17:35:08====================cron2任务开始,开始=2019-10-11 17:35:08 threadId=57, threadName=myScheduling-3 cron1任务正在运行的线程名称:myScheduling-2结束,开始=2019-10-11 17:34:49结束=2019-10-11 17:34:49弹簧引导如何通过@Scheduled实现定时任务及多线程配置