Java簡單定時任務

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

@SuppressWarnings("all")
public class TimeTaskTest {
    public static void main(String[] args) throws Exception {
        // 創建定時任務器
        Timer timer = new Timer();
        // 執行定時任務 5秒執行一次
        timer.schedule(
                new MTimerTask(),
                new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS").parse("2020-4-22 00:50:00 000"),
                5*1000
        );
    }
}

/**
 * 定時任務
 */
@SuppressWarnings("all")
class MTimerTask extends TimerTask {
    @Override
    public void run() {
        // 輸出當前執行時間
        System.out.println("timertask start with:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
    }
}

代碼如上所示,定時任務處理參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章