repeating a task for a length of time

Discussion in 'Java' started by rebelbuttmunch, May 2, 2007.

  1. 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.
    rebelbuttmunch, May 2, 2007
    #1
    1. Advertising

  2. rebelbuttmunch

    Daniel Pitts Guest

    On May 2, 7:07 am, rebelbuttmunch <> 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.
    Daniel Pitts, May 2, 2007
    #2
    1. Advertising

  3. rebelbuttmunch

    Red Orchid Guest

    Daniel Pitts <> wrote or quoted in
    Message-ID <>:

    >
    > 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).
    Red Orchid, May 2, 2007
    #3
  4. rebelbuttmunch

    Daniel Pitts Guest

    On May 2, 9:24 am, "Red Orchid" <> wrote:
    > Daniel Pitts <> wrote or quoted in
    > Message-ID <>:
    >
    >
    >
    > > 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?
    Daniel Pitts, May 3, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor
    Replies:
    0
    Views:
    8,822
    Victor
    Sep 1, 2004
  2. krabhi
    Replies:
    1
    Views:
    9,263
    Marco Meschieri
    Aug 9, 2006
  3. teggy
    Replies:
    0
    Views:
    804
    teggy
    May 29, 2007
  4. Mike
    Replies:
    1
    Views:
    1,919
    GArlington
    May 12, 2008
  5. Stéphane Wirtel
    Replies:
    3
    Views:
    356
    Stephane Wirtel
    Jun 15, 2007
Loading...

Share This Page