Recurring Countdown Timer in Java?

If I were you, I'd use: an AtomicInteger variable which would keep the current countdown value a timer thread that would wake up every 1s and decrementAndGet() the variable, comparing the result to zero and terminating the app if the result is zero (possibly) a thread that would also wake up every 1s to repaint the GUI -- the best approach here depends on your GUI framework Finally, whenever you need to reset the count back to 60s, you just call set(newValue) from any thread The timer thread's run() method could be as simple as: for (;;) { if (counter.decrementAndGet().

If I were you, I'd use: an AtomicInteger variable which would keep the current countdown value; a timer thread that would wake up every 1s and decrementAndGet() the variable, comparing the result to zero and terminating the app if the result is zero; (possibly) a thread that would also wake up every 1s to repaint the GUI -- the best approach here depends on your GUI framework. Finally, whenever you need to reset the count back to 60s, you just call set(newValue) from any thread. The timer thread's run() method could be as simple as: for (;;) { if (counter.decrementAndGet() Sleep(1000); } I think it's much easier to get this right than trying to manage multiple Timer objects.

This has me moving in the right direction. I will have to mess around with it before I really fully understand, but it's a great starting platform. – Speakr Sep 20 at 16:22.

The best way to impliment timer in your application is using some sheduler frameworks like Quartz.

You could use java.util. Timer to schedule an execution of a method and then cancel it if the requirements is met. Like this: timer = new Timer(); timer.

Schedule(new Task(), 60 * 1000); And then make a class like this to handle the timerschedule: class Task extends TimerTask { public void run() { System. Exit(0); } } If the requirements is met, then do this to stop it from executing: timer.cancel().

System. Exit(0); kills the entire VM. Is this the only way to go, or is there a way to register an event that will tell the main frame to kill itself?

– X-Zero Sep 19 at 15:42.

If you need to update your GUI better to use SwingWorker en.wikipedia.org/wiki/SwingWorker I would write something like this: SwingWorker timer = new SwingWorker() { Integer timer=60; @Override protected String doInBackground() throws Exception { //update guiModel //label. SetText(timer.toString()); while(timer>0){ Thread. Sleep(1000); timer--; } return null; } @Override public void done(){ System.

Exit(0); } }; JButton restart = new JButton(){ { addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { timer. Cancel(true); timer.execute(); } }); } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions