Ahmad Abdullah
Ahmad Abdullah

Reputation: 23

How to make a countdown CountDown Timer in java ( stopwatch Java )

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;
    }    
}

I have a problem in this that when I run it doesn't stop from counting!

So, how do I make it stop when counter reaches 0?

Upvotes: 1

Views: 6299

Answers (3)

GhostCat
GhostCat

Reputation: 140427

You need to instruct the timer to stop upon the last iteration; like:

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

See javadoc for further details. You probably don't the call to purge() here, as there is only one thread dealing with that timer; but in more "generic" use cases it would be required.

You might have to change

Timer time = new Timer();

to

final Timer time = new Timer();

(depending on your version of Java) to tell the compiler that it is save to access the outer local variable from within that anonymous inner class.

Upvotes: 2

Dominique Ubersfeld
Dominique Ubersfeld

Reputation: 1

Here is a quick fix for your issue. You have to call the method cancel() as below:

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

Upvotes: 0

P S M
P S M

Reputation: 1161

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;
    }

}

Upvotes: 0

Related Questions