package _4;
import java.util.Timer;
import java.util.TimerTask;
public class TimerThreadDemo {
static class Blossom extends TimerTask{
public void run() {
System.out.println(Thread.currentThread().getName()+":开花");
}
}
static class Fruit extends TimerTask{
public void run() {
System.out.println(Thread.currentThread().getName()+":结果");
}
}
public static void main(String[]args)throws InterruptedException{
Timer timer=new Timer();
timer.schedule(new Blossom(),300,200);
//300ms后开始执行,每200ms执行一次
timer.schedule(new Fruit(),400,500);
//400ms后执行,每500ms执行一次
Thread.sleep(1000);
//主线程休息1000ms
timer.cancel();
//终止定时器,丢弃所有正在执行的任务
}
}
1662

被折叠的 条评论
为什么被折叠?



