今天好奇,把两个定时器嵌套了一下出现了下面的结果,这是为什么
- 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
复制代码
为什么打印次数随时间增长会多打一次
|
|