Kid
Kid

Reputation: 23

Java function to perform task every minute using computer's clock

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!

Upvotes: 1

Views: 243

Answers (2)

Rahul Tripathi
Rahul Tripathi

Reputation: 172468

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

Upvotes: 1

ZeroOne
ZeroOne

Reputation: 3171

Use the ScheduledExecutorService. See a complete example here.

Upvotes: 1

Related Questions