Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Loop every minute

Tags:

java

loops

time

I want to write a loop in Java that firs starts up and goes like this:

while (!x){     //wait one minute or two      //execute code } 

I want to do this so that it does not use up system resources. What is actually going on in the code is that it goes to a website and checks to see if something is done, if it is not done, it should wait another minute until it checks again, and when its done it just moves on. Is their anyway to do this in java?

like image 523
Robert Cardona Avatar asked Apr 24 '10 02:04

Robert Cardona


People also ask

How do you make a time loop in Java?

Just use schedule().

How does Java Timer work?

Timer class provides a method call that is used by a thread to schedule a task, such as running a block of code after some regular instant of time. Each task may be scheduled to run once or for a repeated number of executions.


1 Answers

You can use Timer

Timer timer = new Timer();  timer.schedule( new TimerTask() {     public void run() {        // do your work      }  }, 0, 60*1000); 

When the times comes

  timer.cancel(); 

To shut it down.

like image 50
OscarRyz Avatar answered Sep 20 '22 11:09

OscarRyz