Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > repeating a task for a length of time

Reply
Thread Tools

repeating a task for a length of time

 
 
rebelbuttmunch
Guest
Posts: n/a
 
      05-02-2007
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.

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      05-02-2007
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.

 
Reply With Quote
 
 
 
 
Red Orchid
Guest
Posts: n/a
 
      05-02-2007
Daniel Pitts <> wrote or quoted in
Message-ID <. com>:

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


 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      05-03-2007
On May 2, 9:24 am, "Red Orchid" <windfollowcl...@yahoo.com> wrote:
> Daniel Pitts <googlegrou...@coloraura.com> wrote or quoted in
> Message-ID <1178116602.911032.319...@y5g2000hsa.googlegroups. com>:
>
>
>
> > 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).


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stop org.jdesktop.application.Task started using @Action public Task Mike Java 1 05-12-2008 12:09 PM
[Rake] call a task of a namespace from an other task. Stéphane Wirtel Ruby 3 06-15-2007 06:52 AM
Accessing ant task name when running a task teggy Java 0 05-29-2007 02:20 PM
Maven using ANT plugin for SCP task : Embedded error: Could not create task or type of type: scp. krabhi Java 1 08-09-2006 04:19 PM
Re: Problem in ant replace task and replaceregexp task to update xml Victor Java 0 09-01-2004 03:58 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57