public class Task2 extends TimerTask{
private static Timer timer = new Timer();
public static void main(String[] args) {
Task2 task1 = new Task2();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date time = simpleDateFormat.parse("2017-8-4 20:42:00");
timer.scheduleAtFixedRate(task1, time, 3000);
} catch (ParseException e) {
e.printStackTrace();
}
} @Override
public void run() {
System.out.println("begin--"+new Date());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end-- -"+new Date());
}}public class Task1 extends TimerTask{
private static Timer timer = new Timer();
public static void main(String[] args) {
Task1 task1 = new Task1();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date time = simpleDateFormat.parse("2017-8-4 20:42:00");
timer.schedule(task1, time, 3000);
} catch (ParseException e) {
e.printStackTrace();
}
} @Override
public void run() {
System.out.println("begin--"+new Date());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end-- -"+new Date());
}}
为什么打印出来的结果是一样的begin--Fri Aug 04 20:42:00 GMT+08:00 2017
end-- -Fri Aug 04 20:42:02 GMT+08:00 2017
begin--Fri Aug 04 20:42:03 GMT+08:00 2017
end-- -Fri Aug 04 20:42:05 GMT+08:00 2017
begin--Fri Aug 04 20:42:06 GMT+08:00 2017
end-- -Fri Aug 04 20:42:08 GMT+08:00 2017
begin--Fri Aug 04 20:42:09 GMT+08:00 2017
end-- -Fri Aug 04 20:42:11 GMT+08:00 2017
begin--Fri Aug 04 20:42:12 GMT+08:00 2017
end-- -Fri Aug 04 20:42:14 GMT+08:00 2017