forked from brianway/java-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun4_schedule_vs_scheduleAtFixedRate_1.java
More file actions
50 lines (44 loc) · 1.43 KB
/
Copy pathRun4_schedule_vs_scheduleAtFixedRate_1.java
File metadata and controls
50 lines (44 loc) · 1.43 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
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;
/**
* P260
* schedule不具有追赶执行性
*/
public class Run4_schedule_vs_scheduleAtFixedRate_1 {
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.schedule(task, runDate, 2000);
}
}
/*
输出:
现在执行时间:Sat Apr 16 00:51:36 CST 2016
计划执行时间:Sat Apr 16 00:51:16 CST 2016
begin timer=Sat Apr 16 00:51:36 CST 2016
end timer=Sat Apr 16 00:51:36 CST 2016
begin timer=Sat Apr 16 00:51:38 CST 2016
end timer=Sat Apr 16 00:51:38 CST 2016
begin timer=Sat Apr 16 00:51:40 CST 2016
end timer=Sat Apr 16 00:51:40 CST 2016
begin timer=Sat Apr 16 00:51:42 CST 2016
end timer=Sat Apr 16 00:51:42 CST 2016
*/