如何在java中进行倒计时CountDown Timer(秒表Java)

时间:2017-02-28 13:09:33

标签: java countdowntimer stopwatch

package test;

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

public class StopWatch 
{
    public static int interval;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Input seconds => : ");
        interval = input.nextInt();
        int delay = 1000;
        int period = 1000;
        Timer time = new Timer();
        System.out.println(interval);
        time.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                if (interval == 0) {
                    System.out.println("work finished");
                } else {
                    System.out.println(setInterval());
                }
            }
        }, delay, period);
    }

    private static int setInterval() {          
        return --interval;
    }    
}

我有一个问题,当我运行它时不会停止计数!

那么,当计数器达到0时,如何让它停止?

3 个答案:

答案 0 :(得分:2)

您需要指示计时器在最后一次迭代时停止;像:

public void run() {
   if (interval == 0) {
     System.out.println("work finished");
     time.cancel();
     time.purge();

有关详细信息,请参阅javadoc。你可能不会在这里调用purge(),因为只有一个线程处理该计时器;但在更“通用”的用例中,它是必需的。

您可能需要更改

Timer time = new Timer();

final Timer time = new Timer();

(取决于您的Java版本)告诉编译器保存以从该匿名内部类中访问外部局部变量。

答案 1 :(得分:0)

package com.test;

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

public class StopWatch 
{
    public static int interval;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Input seconds => : ");
        interval = input.nextInt();
        int delay = 1000;
        int period = 1000;
        final Timer time = new Timer();
        System.out.println(interval);
        time.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                if (interval == 0) {
                    System.out.println("work finished");
                    time.cancel();
                    time.purge();
                } else {
                    System.out.println(setInterval());
                }
            }
        }, delay, period);
    }

    private static int setInterval() {

        return --interval;
    }

}

答案 2 :(得分:0)

以下是您的问题的快速解决方法。你必须调用方法cancel(),如下所示:

public static int interval;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Input seconds => : ");
        interval = input.nextInt();
        int delay = 1000;
        int period = 1000;
        Timer time = new Timer();
        System.out.println(interval);
        time.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                if (interval == 0) {
                    System.out.println("work finished");
                    time.cancel();
                } else {
                    System.out.println(setInterval());
                }
            }
        }, delay, period);
    }

    private static int setInterval() {

        return --interval;
    }

} 

Dominique Ubersfeld