Java定时任务Timer与TimerTask

Main

import java.util.Timer;

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

MyTask

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimerTask;

public class MyTask extends TimerTask {
    public void run() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        System.out.println(LocalDateTime.now().format(dateTimeFormatter));
    }
}

posted @ 2020-10-11 20:13  xl4ng  阅读(108)  评论(0编辑  收藏  举报