package demo;
import java.util.Timer;
import java.util.TimerTask;
public class Demo {
public static Timer timer = new Timer();
static {
MyTask task = new MyTask();
timer.schedule(task, 1000);
}
public static void main(String[] args) {
System.out.println("Begin...");
while (true) {
// do something
}
}
}
class MyTask extends TimerTask {
public void run() {
// do something
MyTask task = new MyTask();
Demo.timer.schedule(task, 1000);
}
}
如何使main
线程在进入时暂停并TimerTask
在退出时恢复。