Vincent Roye
Vincent Roye

Reputation: 2851

Java Infinite very long loop

I have to do a Java infinite loop but 1 loop would last 24 hours. What is the best way to do it? How could I kill it? Pause and resume it?

Upvotes: 0

Views: 309

Answers (5)

Frederik.L
Frederik.L

Reputation: 5620

Your question does not really explain what you are trying to do, but I will try to give you some tools to work with.

To run a single loop or a predetermined amount of loops asynchronously, you may achieve what you need with Threads.

If you need more control like how many tasks can be running at the same time, you may use a Thread Pool implementation. This is helpful especially if you don't know how many tasks have to be started and need to restrict memory usage.

I hope this helps.

Upvotes: 0

manocha_ak
manocha_ak

Reputation: 902

"I have to do a Java infinite loop but 1 loop would last 24 hours. " As mentioned if the loops end at some time even if large its not infinite. So, do you mean infinite loop only or the the loop ending 24 hrs? A simple infinite loop is

do{
  //do your stuff
} while (true);

Upvotes: 0

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77474

How about just sleeping for 24 hours?

Thread.sleep(24*60*60*1000);

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258618

You can use the Timer class with the method:

void    schedule(TimerTask task, Date firstTime, long period) 

Upvotes: 2

Matthias Meid
Matthias Meid

Reputation: 12523

Perhaps a while loop, where you check the elapsed time in the loop condition? This may get pretty expensive if the loop body is short, however.

Upvotes: 0

Related Questions