JAVA Timer定时器使用方法(二)

 JAVA  Timer 定时器测试 

MyTask.java:
package com.timer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

public class MyTask extends TimerTask{
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    public void run(){
        System.out.println(sdf.format(new Date()));
    }
}

TestTimer.java:

package com.timer;

import java.util.Timer;

public class TestTimer{
    
    public static void main(String[] args){
        
        MyTask myTask = new MyTask();
        
        Timer timer = new Timer();
        
        timer.schedule(myTask, 1000, 1000);
        
        System.out.println("main end");
    }
    
}

输出结果:
main end
2009-12-31 19:25:58
2009-12-31 19:25:59
2009-12-31 19:26:00
2009-12-31 19:26:01
2009-12-31 19:26:02
2009-12-31 19:26:03

posted on 2014-12-15 19:41  qingchen1984  阅读(168)  评论(0编辑  收藏  举报

导航