(Java)如何在满足某些条件时立即取消计时器

时间:2019-03-02 21:10:16

标签: java timer

程序
我的程序会问三个数学问题,例如10 + 5? 它一次在控制台上显示一个问题。用户通过命令行进行回答,并在 5秒钟内回答。当用户正确回答该问题时,下一个问题将显示为

一旦用户在给定时间内正确回答了问题,下一个问题就需要显示(它不应该等到时间到了)。当用户回答错误的问题时,计时器应该继续并且不重新启动。出现下一个问题时,计时器仅重启

问题
用户正确回答问题后,程序不会立即取消计时器。
同样,即使时间到了,下一个问题也不会出现。用户必须输入一些内容才能继续下一个问题。
最后,当用户输入错误答案时,还会显示下一个问题。

import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask; 
import java.util.Random;
/** 
 *Simple count down timer demo of java.util.Timer facility.
 */

/*
 * start timer
 * cancel when isLastMoveValid == true
 * start timer again soon
 * */

public class Countdown {
    public static double i = 5;
    public static int answer;
    static Scanner inputs = new Scanner(System.in);
    static Random rand = new Random(); 
    static int num1;
    static int num2;
    static int questionNo = 3;
    public static void main(String args[]) {
        while (questionNo >0) {
            num1 = rand.nextInt(11); 
            num2 = rand.nextInt(11);
            callTimer();       
            System.out.println(num1+ "+" + num2 + "?");
            answer = inputs.nextInt();
        }

    } // end of main method

    public static void callTimer() {

         final Timer timer = new Timer();
         i = 6;

         timer.scheduleAtFixedRate(new TimerTask() {          
             public void run() {
                 i -= 0.001;

                 if (i< 0 || answer == num1 + num2) {
                     questionNo--;              
                     timer.cancel();                
                 }

             } // end of run
         }, 0, 1); // end of scheduleAtFixedRate



    } // end of callTimer
}

1 个答案:

答案 0 :(得分:0)

您需要将计时器对象作为字段,因此您可以随时访问。请参见cancel方法如何取消。如果要重新启动计时器,则必须创建计时器和timertask的新对象,因为它们会被线程丢弃。

请参阅有关Timer.cancel方法的文档:

  

终止此计时器,放弃当前计划的所有任务。   不会干扰当前正在执行的任务(如果存在)。   计时器已终止,其执行线程正常终止,   并且无法再安排任何任务。

例如这样的东西:

import java.util.Timer;
import java.util.TimerTask;

public class TimerTest
{
    private Timer timer;

    public void cancelTimer()
    {
        this.timer.cancel();
        System.out.println("Canceled timer!");
    }

    public void startTimer()
    {
        // once a timertask is canceled, you can not restart it
        // because the thread is deleted. So we need to create a 
        // new object of a timer and a timertask.
        TimerTask timerTask = new TimerTask()
        {
            @Override
            public void run()
            {
                System.out.println("Hello there!");
            }
        };
        this.timer = new Timer();
        System.out.println("Starting timer ...");
        int period = 1000;
        this.timer.schedule(timerTask, 0, period);
    }

    public static void main(String[] args) throws InterruptedException
    {
        TimerTest tt = new TimerTest();
        tt.startTimer();
        Thread.sleep(5000);
        tt.cancelTimer(); // you can call this method, as soon as u have a correct answer
        Thread.sleep(1000);
        tt.startTimer(); // you can restart your timer as demanded
        Thread.sleep(5000);
        tt.cancelTimer(); // and cancel it again
    }
}