repeating a task for a length of time

R

rebelbuttmunch

Hi,

What would be the best way to code a task so that it would run
iteratively for n seconds. For example, I have to code to fire a http
request, but I want to fire that request over and over for 60 seconds.
I was thinking about grabbing the time at the start and checking the
time after each loop, but it seems a bit innefficient.

Thanks!
Stephen.
 
D

Daniel Pitts

Hi,

What would be the best way to code a task so that it would run
iteratively for n seconds. For example, I have to code to fire a http
request, but I want to fire that request over and over for 60 seconds.

So, your requirement (pseudo code)

for 60 second
make a request


in Java, that translates to
I was thinking about grabbing the time at the start and checking the
time after each loop, but it seems a bit innefficient.

long endTime = System.currentTimeMillis() + 60 * 1000;
while (endTime > System.currentTimeMillis()) {
makeRequest();
}

Whats inefficient about that? It does exactly what you want, and
nothing you don't. It would be a little different if you didn't do
anything in the while loop. Some people mistakenly use that as a
delay/sleep mechanism.

// BAD:
while (endTime > System.currentTimeMillis()) { /* do nothing */}

Hope this helps.
Daniel.
 
R

Red Orchid

Message-ID said:
long endTime = System.currentTimeMillis() + 60 * 1000;
while (endTime > System.currentTimeMillis()) {
makeRequest();
}


I think that the following code is only proper for simple testing.

<code>
long startTime = System.currentTimeMillis();

.... // code block #1

long endTime = System.currentTimeMillis();
long interval = endTime - startTime;
</code>

"interval" is unreliable value because "System.currentTimeMillis()"
returns current time of *system*. In other words, if system time
is set to 01/01/1970 during "#1", the value of "interval" is negative.

Modern clock programs have the function of automatic synchronization
with internet time server (e.g. clock on WinXP tray).
 
D

Daniel Pitts

Message-ID <[email protected]>:




I think that the following code is only proper for simple testing.

<code>
long startTime = System.currentTimeMillis();

... // code block #1

long endTime = System.currentTimeMillis();
long interval = endTime - startTime;
</code>

"interval" is unreliable value because "System.currentTimeMillis()"
returns current time of *system*. In other words, if system time
is set to 01/01/1970 during "#1", the value of "interval" is negative.

Modern clock programs have the function of automatic synchronization
with internet time server (e.g. clock on WinXP tray).

How about System.nanoTime() then? That has a guaranty on the
difference between two calls, does it not?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top