首页 新闻 搜索 专区 学院

spring定时器调度机制?

0
悬赏园豆:50 [已关闭问题] 关闭于 2017-05-17 17:30

以下是基于spring定时器串行的测试:

class Timmer01{
private static int num = 0; @scheduled(cron
="5,10,15,20 * * * * *"public void timmer(){ log.info(++num + ","+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
Thread.sleep(3*1000); } }
class Timmer02{ @scheduled(cron="5,10,15,20 * * * * *"public void timmer(){ log.info(++num + ","+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
Thread.sleep(3*1000);
} }
class Timmer03{ @scheduled(cron="5,10,15,20 * * * * *"public void timmer(){ log.info(++num + ","+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
Thread.sleep(3*1000);
} }
class TimmerNew{
private static int num = 0; @scheduled(cron
="8,16,24 * * * * *"public void timmer(){ log.info(++num + ","+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
Thread.sleep(1*1000);
} }
复制代码
01:13:05.001  --- [pool-1-thread-1] Timmer02 : spring timmer 02 : 1 , 01:13:05
01:13:08.003  --- [pool-1-thread-1] Timmer03 : spring timmer 03 : 1 , 01:13:08
01:13:11.003  --- [pool-1-thread-1] Timmer01 : spring timmer 01 : 1 , 01:13:11
01:13:14.004  --- [pool-1-thread-1] TimmerNew : new task : 1 , 01:13:14
01:13:15.004  --- [pool-1-thread-1] Timmer02 : spring timmer 02 : 2 , 01:13:15
01:13:18.004  --- [pool-1-thread-1] Timmer01 : spring timmer 01 : 2 , 01:13:18
01:13:21.004  --- [pool-1-thread-1] Timmer03 : spring timmer 03 : 2 , 01:13:21
01:13:24.008  --- [pool-1-thread-1] TimmerNew : new task : 2 , 01:13:24
01:13:25.009  --- [pool-1-thread-1] Timmer02 : spring timmer 02 : 3 , 01:13:25
(以上第一分钟内的log记录被我重新排版了,方便阅读)
2017-03-20 01:14:05.000 INFO 7012 --- [pool-1-thread-1] c.v.demo.spring.scheduling.Timmer01 : spring timmer 01 : 3 , 01:14:05 2017-03-20 01:14:08.001 INFO 7012 --- [pool-1-thread-1] c.v.demo.spring.scheduling.Timmer02 : spring timmer 02 : 4 , 01:14:08 2017-03-20 01:14:11.001 INFO 7012 --- [pool-1-thread-1] c.v.demo.spring.scheduling.Timmer03 : spring timmer 03 : 3 , 01:14:11 2017-03-20 01:14:14.001 INFO 7012 --- [pool-1-thread-1] c.v.demo.spring.scheduling.TimmerNew : new task : 3 , 01:14:14 2017-03-20 01:14:15.002 INFO 7012 --- [pool-1-thread-1] c.v.demo.spring.scheduling.Timmer01 : spring timmer 01 : 4 , 01:14:15 2017-03-20 01:14:18.002 INFO 7012 --- [pool-1-thread-1] c.v.demo.spring.scheduling.Timmer02 : spring timmer 02 : 5 , 01:14:18 2017-03-20 01:14:21.002 INFO 7012 --- [pool-1-thread-1] c.v.demo.spring.scheduling.Timmer03 : spring timmer 03 : 4 , 01:14:21 2017-03-20 01:14:24.005 INFO 7012 --- [pool-1-thread-1] c.v.demo.spring.scheduling.TimmerNew : new task : 4 , 01:14:24 2017-03-20 01:14:25.005 INFO 7012 --- [pool-1-thread-1] c.v.demo.spring.scheduling.Timmer01 : spring timmer 01 : 5 , 01:14:25
复制代码

(结果中1、2、3是定时器被执行的次数,后面是时间。)

猜想了几种spring定时器的调度顺序,都没有推出这执行结果。求教针对串行,spring是怎么调度的?

主要是每分钟中,最后有单独1次的timmer01/2/3被执行

淡丶无欲的主页 淡丶无欲 | 初学一级 | 园豆:87
提问于:2017-03-20 13:05
< >
分享
所有回答(1)
0

定时器嘛,就是线程问题,你研究一下java timer,定时器就是定时触发你的请求,是一个异步程序。。。

全力以赴001 | 园豆:623 (小虾三级) | 2017-03-20 17:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册