从洛阳到重庆的火车票:按要求修改JAVA程序

来源:百度文库 编辑:神马品牌网 时间:2024/05/03 01:58:53
import java.util.*;

public class TimerDemo {

public static void main(String[] args) {
class MyTimerTask extends TimerTask //将该类放于main之外、TimerDemo类中,实现同样功能
{
private Timer tm = null;
public MyTimerTask(Timer tm)
{
this.tm = tm;
}
public void run()
{
try
{
Runtime.getRuntime().exec("calc.exe");
}
catch (Exception ex)
{
ex.printStackTrace();
}
tm.cancel();

}

}
Timer tm = new Timer();
tm.schedule(new MyTimerTask(tm),3000,2000);
}
}

import java.util.*;

public class TimerDemo {

static Timer tm = new Timer();

public static class MyTimerTask extends TimerTask
{
private Timer tm = null;

public MyTimerTask(Timer tm) {
this.tm = tm;
}

public void run() {
try {
Runtime.getRuntime().exec("calc.exe");
} catch (Exception ex) {
ex.printStackTrace();
}
tm.cancel();

}

}

public static void main(String[] args) {
tm.schedule(new TimerDemo.MyTimerTask(tm), 3000, 2000);
}
}

在main中创建MyTimerTask 的实例就行了吧