我想以这种方式安排我的工作: 1)只有一个线程运行它。 (串行) 2)两次运行之间有固定间隔
我在我的方法上使用@Scheduled(fixedDelay = 10000)
,在我的applicationContext.xml中使用<task:annotation-driven/>
。但根据我的打印信息,我设置的两个作业之间的间隔不是10000毫秒。那是为什么?
19:07-25 22:38:46.190 INFO schedule:33 [21-thread-1] - [job#1] is running. -----> 1469457526190
20:07-25 22:38:48.191 INFO schedule:45 [21-thread-1] - [job#1] use 2000ms
21:07-25 22:38:48.191 INFO schedule:47 [21-thread-1] - [job#1] is finished. -----> 1469457528191
25:07-25 22:39:03.198 INFO schedule:33 [21-thread-1] - [job#1] is running. -----> 1469457543198
26:07-25 22:39:05.201 INFO schedule:45 [21-thread-1] - [job#1] use 2002ms
27:07-25 22:39:05.202 INFO schedule:47 [21-thread-1] - [job#1] is finished. -----> 1469457545202
31:07-25 22:39:20.205 INFO schedule:33 [21-thread-1] - [job#1] is running. -----> 1469457560205
32:07-25 22:39:22.209 INFO schedule:45 [21-thread-1] - [job#1] use 2004ms
33:07-25 22:39:22.210 INFO schedule:47 [21-thread-1] - [job#1] is finished. -----> 1469457562210
37:07-25 22:39:37.213 INFO schedule:33 [21-thread-1] - [job#1] is running. -----> 1469457577213
38:07-25 22:39:39.215 INFO schedule:45 [21-thread-1] - [job#1] use 2002ms
39:07-25 22:39:39.215 INFO schedule:47 [21-thread-1] - [job#1] is finished. -----> 1469457579215
43:07-25 22:39:54.221 INFO schedule:33 [21-thread-1] - [job#1] is running. -----> 1469457594221
44:07-25 22:39:56.225 INFO schedule:45 [21-thread-1] - [job#1] use 2004ms
45:07-25 22:39:56.225 INFO schedule:47 [21-thread-1] - [job#1] is finished. -----> 1469457596225
49:07-25 22:40:11.525 INFO schedule:33 [21-thread-1] - [job#1] is running. -----> 1469457611525
50:07-25 22:40:13.528 INFO schedule:45 [21-thread-1] - [job#1] use 2003ms
51:07-25 22:40:13.529 INFO schedule:47 [21-thread-1] - [job#1] is finished. -----> 1469457613529
55:07-25 22:40:28.237 INFO schedule:33 [21-thread-1] - [job#1] is running. -----> 1469457628237
你可以看到完成和运行之间的时间不是10000毫秒。 (更像是15000)
代码是(春季4.2.1):
@Scheduled(fixedDelay = 10000)
public void timerJob1(){
SCHEDULER.info("[job#1] is running. -----> {}", System.currentTimeMillis());
Stopwatch stopwatch = Stopwatch.createStarted();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
long use = stopwatch.elapsed(TimeUnit.MILLISECONDS);
SCHEDULER.info("[timeJob1] use {}ms", use);
}
long use = stopwatch.elapsed(TimeUnit.MILLISECONDS);
SCHEDULER.info("[job#1] use {}ms", use);
SCHEDULER.info("[job#1] is finished. -----> {}", System.currentTimeMillis());
}
答案 0 :(得分:0)
您可能会对正在使用的线程池产生争用。创建自己的执行程序,通过实现和连接ScheduledConfigurer来处理这些固定的延迟任务。
在configureTasks()的实现中,将自己的Executor设置为方法参数ScheduledTaskRegistrar上的调度程序。