Repository navigation
Expand file tree
/
Copy pathRun4_schedule_vs_scheduleAtFixedRate_2.java
More file actions
69 lines (64 loc) · 2.25 KB
/
Copy pathRun4_schedule_vs_scheduleAtFixedRate_2.java
File metadata and controls
69 lines (64 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.brianway.learning.java.multithread.timer.example4;
/**
* Created by brian on 2016/4/15.
*/
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* P261
* scheduleAtFixedRate具有追赶执行性
*/
public class Run4_schedule_vs_scheduleAtFixedRate_2 {
static public class MyTask extends TimerTask {
@Override
public void run() {
System.out.println("begin timer=" + new Date());
System.out.println("end timer=" + new Date());
}
}
public static void main(String[] args) {
MyTask task = new MyTask();
System.out.println("现在执行时间:" + new Date());
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) - 20);
Date runDate = calendar.getTime();
System.out.println("计划执行时间:" + runDate);
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, runDate, 2000);
}
}
/*
输出:
现在执行时间:Sat Apr 16 00:54:46 CST 2016
计划执行时间:Sat Apr 16 00:54:26 CST 2016
begin timer=Sat Apr 16 00:54:46 CST 2016
end timer=Sat Apr 16 00:54:46 CST 2016
begin timer=Sat Apr 16 00:54:46 CST 2016
end timer=Sat Apr 16 00:54:46 CST 2016
begin timer=Sat Apr 16 00:54:46 CST 2016
end timer=Sat Apr 16 00:54:46 CST 2016
begin timer=Sat Apr 16 00:54:46 CST 2016
end timer=Sat Apr 16 00:54:46 CST 2016
begin timer=Sat Apr 16 00:54:46 CST 2016
end timer=Sat Apr 16 00:54:46 CST 2016
begin timer=Sat Apr 16 00:54:46 CST 2016
end timer=Sat Apr 16 00:54:46 CST 2016
begin timer=Sat Apr 16 00:54:46 CST 2016
end timer=Sat Apr 16 00:54:46 CST 2016
begin timer=Sat Apr 16 00:54:46 CST 2016
end timer=Sat Apr 16 00:54:46 CST 2016
begin timer=Sat Apr 16 00:54:46 CST 2016
end timer=Sat Apr 16 00:54:46 CST 2016
begin timer=Sat Apr 16 00:54:46 CST 2016
end timer=Sat Apr 16 00:54:46 CST 2016
begin timer=Sat Apr 16 00:54:46 CST 2016
end timer=Sat Apr 16 00:54:46 CST 2016
begin timer=Sat Apr 16 00:54:48 CST 2016
end timer=Sat Apr 16 00:54:48 CST 2016
begin timer=Sat Apr 16 00:54:50 CST 2016
end timer=Sat Apr 16 00:54:50 CST 2016
begin timer=Sat Apr 16 00:54:52 CST 2016
end timer=Sat Apr 16 00:54:52 CST 2016
*/