Timer这个类里有停止的方法啊
你看看API,好象是stop

解决方案 »

  1.   

    java.util Class Time
     void cancel()           Terminates this timer, discarding any currently scheduled task.Timer tTimer = new Timer();
    tTimer.schedule(new TimerTask(),firstDate);tTimer.cancel();
      

  2.   

    咦,太多空格了。其实就是说用Timer的cancel()方法,就可以中止timer,并丢弃所有在这个timer里设定的任务。
      

  3.   

    timer.cancel();
    timer = null;
      

  4.   

    问题是如果直接调用cancel,指定的任务就不能执行了。我是想当TimerTask 在预定时间执行完一次任务后,自动终止Timer和TimerTask. 
    请各位高手帮忙!多谢!!!
      

  5.   

    你可以考虑把Timer的引用带到那个要执行的task里,然后调用timer.cancel()。
    实现应该没问题就是看起来有点别扭。
      

  6.   

    在Task里面去终止Time的定义。
      

  7.   

    import java.util.Timer;
    import java.util.TimerTask;public class EggTimer {
        private final Timer timer = new Timer();
        private final int minutes;    public EggTimer(int minutes) {
            this.minutes = minutes;
        }    public void start() {
            timer.schedule(new TimerTask() {
                public void run() {
                    playSound();
                    timer.cancel();
                }
                private void playSound() {
                    System.out.println("Your egg is ready!");
                    // Start a new thread to play a sound...
                }
            }, minutes * 60 * 1000);
        }    public static void main(String[] args) {
            EggTimer eggTimer = new EggTimer(2);
            eggTimer.start();
        }}
      

  8.   

    playSound();
    timer.cancel();
    不是一样马上就取消了吗?