new Timer().schedule(new MyTimerTask(), 2000); // 没有MyTimerTask
将两个内部类拿到main之外!
改为如下就可以运行!
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TraditionalTimerTest {
public static void main(String[] args) {
new Timer().schedule(new MyTimerTask_a(), 2000);
while (true) {
System.out.println(new Date().getSeconds());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class MyTimerTask_a extends TimerTask {
public void run() {
System.out.println("bombing!");
new Timer().schedule(new MyTimerTask_b(), 4000);
}
}
class MyTimerTask_b extends TimerTask {
public void run() {
System.out.println("bombing!");
new Timer().schedule(new MyTimerTask_a(), 2000);
}
}