pangyingxz 2010-07-20 10:21
浏览 232
已采纳

timer怎么停止

public class Time {
private final Timer timer = new Timer();

private final int minutes;

public Time(int minutes) {
    this.minutes = minutes;
}

public void start() {
    timer.schedule(new TimerTask() {
        public void run() {
            playSound();
            timer.cancel();
            Time eggTimer = new Time(1);
            eggTimer.start();
        }

        private void playSound() {
            System.out.println("Your egg is ready!");
        }
    }, minutes * 1 * 1000);
}

public static void main(String[] args) {
        Time eggTimer = new Time(1);
        eggTimer.start();
}

}

在playSound()里面获得键盘的输入让他停止

  • 写回答

1条回答 默认 最新

  • iteye_7333 2010-07-22 10:11
    关注

    第一点:你的循环运行timer不重新新建一个Time。
    用schedule(TimerTask task, long delay, long period)这个方法就行。

    第二点:你要停止,首先要确定在什么样的条件满足的时候,你的time停下。如果在运行多少次后停止下来,可以在Timer中设一个变量Count,给它一个初值,每次运行减一,到0后就cancel Timer就行了。如果你要一段时间后停止,可以在另外一个线程中延迟一段时间后,cancel这个timer,这个不太准,因为另外的线程在拿到cpu主动权后才能cancel这个Timer。给你一个利用主线程延迟的例子吧:
    public class Time {
    private final static Timer timer = new Timer();

    private final int minutes; 
    
    public Time(int minutes) { 
        this.minutes = minutes; 
    } 
    
    public void start() { 
        timer.schedule(new TimerTask() {
            public void run() { 
                playSound(); 
    

    // timer.cancel();
    // Time eggTimer = new Time(1);
    // eggTimer.start();
    }

            private void playSound() { 
                System.out.println("Your egg is ready!"); 
            }
        }, minutes * 1 * 1000, 1000); 
    }
    
    public static void main(String[] args) {
        Time eggTimer = new Time(1); 
        eggTimer.start();
        try {
            Thread.sleep(5000);
            timer.cancel();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } 
    

    }

    当然,如果只用两个线程,也可以直接把Timer设置成后台线程,这样主线程退出时就会自动退出了。如:
    public class Time {
    private final Timer timer = new Timer(true);

    private final int minutes; 
    
    public Time(int minutes) { 
        this.minutes = minutes; 
    } 
    
    public void start() { 
        timer.schedule(new TimerTask() {
            public void run() { 
                playSound(); 
    

    // timer.cancel();
    // Time eggTimer = new Time(1);
    // eggTimer.start();
    }

            private void playSound() { 
                System.out.println("Your egg is ready!"); 
            }
        }, minutes * 1 * 1000, 1000); 
    }
    
    public static void main(String[] args) {
        Time eggTimer = new Time(1); 
        eggTimer.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } 
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)