On May 2, 7:07 am, rebelbuttmunch <stephn...@aol.com> wrote:
> 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.
|