package day09;
import java.util.Timer;
import java.util.TimerTask;
//定时器演示
public class TimerDemo extends Timer{
//第一个定时任务---定义在main方法外,用task引用来指向该匿名类实例对象
TimerTask task = new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("my task run");
}
};
public static void main(String[] args) {
//定时器
Timer timer = new Timer();
//将定时任务交给定时器时回调执行
timer.schedule(new TimerDemo().task, 1000,1000);
//第二个任务,还是用之前的task引用来指向该匿名类实例对象
new TimerDemo().task = new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
timer.cancel();
}
};
//十秒后停止
timer.schedule(new TimerDemo().task, 10000);
}
}
luckIsAttitude
相关分类