[Java 11] Timer 定时调度 入门
TimerTask
package com.qunar.basicJava.javase.p11CommonlyUsedClass.timer; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimerTask; /** * Author: libin.chen@qunar.com Date: 14-6-5 09:24 */ public class MyTask extends TimerTask { @Override public void run() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); System.out.println(simpleDateFormat.format(new Date())); } }TestTask
package com.qunar.basicJava.javase.p11CommonlyUsedClass.timer; import java.util.Timer; /** * Author: libin.chen@qunar.com Date: 14-6-5 09:25 */ public class TestTask { public static void main(String[] args) { Timer timer = new Timer(); MyTask myTask = new MyTask(); timer.schedule(myTask, 1000, 2000); // 从第 1 秒 开始执行,每 2 秒 执行一次 } }