0

how are you guys

I've searched about Timer and how it's working in java

I found this easy way which is making for loop then add this commend

Thread.sleep(1000);

this commend will make the loop stop for 1 second

BUT

what I want to do is to make an app that looks like a question game

the contestant will see the question and choose the right answer but the timer should be working in the same time

if I used this way ( Thread.sleep(1000) ) every thing in the app will stop until the timer finish

what do I do now !?

Raptor
  • 53,206
  • 45
  • 230
  • 366
sulaiman
  • 311
  • 2
  • 3
  • 11
  • Code please my good man! – christopher May 30 '13 at 10:16
  • 3
    [example](http://stackoverflow.com/questions/13691339/adding-a-timer-and-displaying-label-text/13691413#13691413), [example](http://stackoverflow.com/questions/13812205/java-swing-change-text-after-delay/13812311#13812311), [example](http://stackoverflow.com/questions/13888026/java-clock-isnt-counting-in-swing/13888101#13888101), [example](http://stackoverflow.com/questions/14678750/java-label-timer-and-saving/14678873#14678873) – MadProgrammer May 30 '13 at 10:16
  • What should the timer do?? – Gary Klasen May 30 '13 at 10:16
  • why don't you use one more timer and calculate the total time minus second timer value? – orezvani May 30 '13 at 10:27

1 Answers1

0

Something like that : you can do other things while waiting the specified time to execute remindTask()

public class Reminder {
    Timer timer;

    public Reminder(int seconds) {
        timer = new Timer();
        timer.schedule(new RemindTask(), seconds*1000);
        //do other things
    }

    class RemindTask extends TimerTask {
        public void run() {
            System.out.format("Time's up!%n");
            timer.cancel(); //Terminate the timer thread
        }
    }

    public static void main(String args[]) {
        new Reminder(5);
        System.out.format("Task scheduled.%n");
    }
}
h.meknassi
  • 189
  • 8