5.1.3方法schedule(TimerTask task,long delay)的測(cè)試
該方法的作用是以執(zhí)行schedule(TimerTask task,long delay)方法當(dāng)前的時(shí)間為參考時(shí)間首懈,在此時(shí)間基礎(chǔ)上延遲指定的毫秒數(shù)后執(zhí)行一次TimerTask任務(wù)莲趣。
/**
* @author wuyoushan
* @date 2017/3/20.
*/
public class Run {
static public class MyTask extends TimerTask {
@Override
public void run() {
System.out.println("運(yùn)行了!時(shí)間為:" +new Date());
}
}
public static void main(String[] args) {
Timer timer = new Timer();
MyTask task = new MyTask();
System.out.println("當(dāng)前時(shí)間:"+new Date().toString());
timer.schedule(task, 7000);
}
}
任務(wù)task被延遲7秒執(zhí)行惜姐。程序的運(yùn)行結(jié)果為:
當(dāng)前時(shí)間:Fri May 19 08:07:08 CST 2017
運(yùn)行了额港!時(shí)間為:Fri May 19 08:07:15 CST 2017
摘選自 java多線程核心編程技術(shù)-5.1.3