1

Can someone please help me? I have this java program that needs to perform a task every minute of the computer's clock.

May I know if this is possible, and if yes what is the best way to do this?

Thank you very much!

Kid
  • 23
  • 4
  • possible duplicate of [Java Thread every X seconds](http://stackoverflow.com/questions/3541676/java-thread-every-x-seconds) – ZeroOne Sep 04 '14 at 09:33
  • another possible dupe [How to schedule a periodic task in Java?](http://stackoverflow.com/questions/7814089/how-to-schedule-a-periodic-task-in-java) – Baby Sep 04 '14 at 09:35

2 Answers2

1

Use the ScheduledExecutorService. See a complete example here.

Community
  • 1
  • 1
ZeroOne
  • 3,041
  • 3
  • 31
  • 52
1

You may try this:

int delay = 60000;   // delay for 1 min.
int period = 5000;  // repeat every 5 sec.
Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            // Task here ...
        }
    }, delay, period);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331