Java多线程中Timer定时器执行完任务为什么不停止?
public Timer(String name) {
thread.setName(name);
thread.start();
}
这是Timer类中调用的构造方法,那我自己写一个简单的线程程序时,run方法运行结束就停止了,为什么这个定时器中的方法运行完以后却不停止呢?
public class MyThread extends Thread {
@Override
public void run() {
super.run();
System.out.println("MyThread");
}
}
public class MyThreadTest {
public static void main(String[] args) {
MyThread mt = new MyThread();
mt.start();
System.out.println("运行结束");
}
}
public class MyTask extends TimerTask{
@Override
public void run() {
System.out.println("任务执行了,时间为:"+new Date());
}
}
public class Test1 {
public static void main(String[] args) {
System.out.println("当前时间为:"+new Date());
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND,calendar.get(Calendar.SECOND)-10);
Date runDate = calendar.getTime();
System.out.println("计划时间为:"+runDate);
MyTask task = new MyTask();
Timer timer = new Timer();
timer.schedule(task,runDate);
}
}
谢谢指点一下是什么原因?