定時(shí)器Timer與TimerTask
目錄:1.代碼 2.動態(tài)圖
1.實(shí)例代碼:
package com.wuage.clm.web;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* 類TimerTest.java的實(shí)現(xiàn)描述:定時(shí)器
*
* @author Administrator 2017年8月17日 上午9:04:59
*/
public class TimerTest {
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
public static void main(String[] args) {
String timingTime = "2017-08-17 12:55:00 000";// 定時(shí)執(zhí)行時(shí)間
Long timeInterval = 10 * 1000L;// 時(shí)間間隔多長時(shí)間執(zhí)行一次
Timer t = new Timer();
try {
t.schedule(new LogTimerTask(), sdf.parse(timingTime), timeInterval);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 類TimerTest.java的實(shí)現(xiàn)描述:執(zhí)行任務(wù)
*
* @author Administrator 2017年8月17日 下午12:52:54
*/
class LogTimerTask extends TimerTask {
@Override
public void run() {
System.out.println("測試定時(shí)器:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date()));
}
}
2.動態(tài)圖演示: