Timer线程
请求大神帮我解答,我想让计算器启动后Timer线程自动结束,可是在不关闭计算器时timer不结束?import
import java.util.Timer;
import java.util.TimerTask;
public class TestTimer {
class MyTimerTask extends TimerTask {
private Timer t ;
public MyTimerTask(Timer t) {
this.t = t;
}
public void run() {
try {
//启动计算器程序
Runtime.getRuntime().exec("calc.exe");
} catch (IOException e) {
e.printStackTrace();
}
//结束线程
t.cancel();
}
}
public static void main(String[] args) {
TestTimer tt = new TestTimer();
Timer t = new Timer();
//2秒后执行Timertask
t.schedule(tt.new MyTimerTask(t), 2000);
}
}