首页 > 代码库 > Timer和TimerTask

Timer和TimerTask

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

class MyTask extends TimerTask {

    public void run() {
        SimpleDateFormat sdf = null;
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        System.out.println("The system time now is: " + sdf.format(new Date()));

    }

}

public class TestTask {
    public static void main(String[] args) {
        Timer t = new Timer();
        MyTask myTask = new MyTask();
        t.schedule(myTask, 1000, 2000);
    }
}