package my;import java.util.Timer;public class TimeTaskDemo { /**
* @param args
*/
public static void main(String[] args) {
Timer t=new Timer();
t.schedule(new MyTimeTask(), 5000);
}
}package my;import java.io.IOException;
import java.util.TimerTask;public class MyTimeTask extends TimerTask { @Override
public void run() {
System.out.println("abc");
}}
1.为什么主程序不能结束。
2.在Timer(boolean isDaemon) 构造函数中有这么一句话,其中的相关线程指的是什么线程。Timer类也没有继承多线程啊
Timer(boolean isDaemon)
Creates a new timer whose associated thread may be specified to run as a daemon.
不作为守护进程去运行,
new Timer().schedule(new TimerTask() { @Override
public void run() {
throw new RuntimeException();
}
}, 1000);
System.out.println("main thread over!");主线程也会停止运行的。