Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

repeating a task for a length of time

Thread view: 
rebelbuttmunch - 02 May 2007 15:07 GMT
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.
Daniel Pitts - 02 May 2007 15:36 GMT
> 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.
Red Orchid - 02 May 2007 17:24 GMT
Daniel Pitts <googlegroupie@coloraura.com> wrote or quoted in
Message-ID <1178116602.911032.319830@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).
Daniel Pitts - 03 May 2007 02:49 GMT
> Daniel Pitts <googlegrou...@coloraura.com> wrote or quoted in
> Message-ID <1178116602.911032.319...@y5g2000hsa.googlegroups.com>:
[quoted text clipped - 21 lines]
> 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?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.