Posted on 2006-11-23 14:40
samfree 阅读(235)
评论(0) 编辑 收藏
package com.time;
import java.io.IOException;
import java.util.*;
public class TimerTaskDemo {
private Timer timer;
public TimerTaskDemo(){}
public TimerTaskDemo(Timer timer){
this.timer = timer;
timer.schedule(new MyTask(),3000);
}
public static void main(String[] args) throws IOException{
Timer timer = new Timer();
TimerTaskDemo timerDemo = new TimerTaskDemo(timer);
}
class MyTask extends TimerTask {//TimerTask 实现了Runable接口
public void run() {
// TODO 自动生成方法存根
System.out.println("Task is executed!");
timer.cancel();
}
}
}
三秒钟后输出Task is executed!