Timer

package cn.itcast.nsfw.complain;

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

public class MyTimerTask extends TimerTask {

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

}

package cn.itcast.nsfw.complain;

import java.util.Timer;

public class MyTimer {

    public static void main(String[] args) {
        Timer timer = new Timer();
        //延迟2秒执行,每3秒执行一次
        timer.schedule(new MyTimerTask(), 2000, 3000);
        
    }

}

原文地址:https://www.cnblogs.com/god-monk/p/6519144.html