黑马程序员技术交流社区
标题:
定时器嵌套的结果
[打印本页]
作者:
Mr.Hao
时间:
2014-7-6 14:46
标题:
定时器嵌套的结果
今天好奇,把两个定时器嵌套了一下出现了下面的结果,这是为什么
package com.day7;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TimerDemo {
public static void main(String[] args) {
Timer t = new Timer();
t.scheduleAtFixedRate(
new TimerTask(){
public void run() {
Timer tt = new Timer();
tt.schedule(
new TimerTask(){
public void run() {
SimpleDateFormat s = null;
s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(s.format(new Date())+"..."+2);
}},
0,
1000);
SimpleDateFormat s = null;
s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(s.format(new Date())+"..."+1);
}},
0,
1000);
}
}
复制代码
结果是:
2014-07-06 14:40:55...2
2014-07-06 14:40:55...1
2014-07-06 14:40:56...2
2014-07-06 14:40:56...1
2014-07-06 14:40:56...2
2014-07-06 14:40:57...2
2014-07-06 14:40:57...1
2014-07-06 14:40:57...2
2014-07-06 14:40:57...2
2014-07-06 14:40:58...1
2014-07-06 14:40:58...2
2014-07-06 14:40:58...2
2014-07-06 14:40:58...2
2014-07-06 14:40:58...2
2014-07-06 14:40:59...2
2014-07-06 14:40:59...1
2014-07-06 14:40:59...2
2014-07-06 14:40:59...2
2014-07-06 14:40:59...2
2014-07-06 14:40:59...2
2014-07-06 14:41:00...2
2014-07-06 14:41:00...2
2014-07-06 14:41:00...1
2014-07-06 14:41:00...2
2014-07-06 14:41:00...2
2014-07-06 14:41:00...2
2014-07-06 14:41:00...2
2014-07-06 14:41:01...1
2014-07-06 14:41:01...2
2014-07-06 14:41:01...2
2014-07-06 14:41:01...2
2014-07-06 14:41:01...2
2014-07-06 14:41:01...2
2014-07-06 14:41:01...2
2014-07-06 14:41:01...2
2014-07-06 14:41:02...2
2014-07-06 14:41:02...1
2014-07-06 14:41:02...2
2014-07-06 14:41:02...2
2014-07-06 14:41:02...2
2014-07-06 14:41:02...2
2014-07-06 14:41:02...2
2014-07-06 14:41:02...2
2014-07-06 14:41:02...2
复制代码
为什么打印次数随时间增长会多打一次
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2