Timer with variable delay

Discussion in 'Java' started by crash.test.dummy, Jul 6, 2006.

  1. i have a scheduled task that runs every 5 mins, if everything went ok.
    if the last execution of this scheduled task returned false, the delay
    should be 10 mins.
    how do i do this?

    here's a code sample (using 5-minute [fixed-rate] delays):

    java.util.Timer myTimer = new java.util.Timer();
    myTimer.schedule(new MyRunner(), 0, 300000);

    ....

    static class MyRunner extends TimerTask {
    public void run() {
    FileOpenTask myTask= new FileOpenTask();
    myTask.openFile("myFile.txt");
    }
    }

    .....

    public class FileOpenTask {
    // the returned value of this method is, at this point, useless to
    the caller
    public boolean openFile(String filename) {
    try {
    FileInputStream fstream = new FileInputStream(filename);
    } catch (FileNotFoundException fnfe) {
    return false;
    }
    return true;
    }
    }
     
    crash.test.dummy, Jul 6, 2006
    #1
    1. Advertising

  2. crash.test.dummy

    Oliver Wong Guest

    "crash.test.dummy" <> wrote in message
    news:...
    >i have a scheduled task that runs every 5 mins, if everything went ok.
    > if the last execution of this scheduled task returned false, the delay
    > should be 10 mins.
    > how do i do this?


    How about having the task take care of scheduling its next invocation?

    So when you first want to run the task, you tell it to run right now,
    without any timer stuff. The tasks runs, doing its main work. Once the main
    work is done, it decides whether it needs to run 5 minutes later or 10
    minutes later, and schedules itself appropriately to run once (as opposed to
    running indefinitely with a fixed interval).

    - Oliver
     
    Oliver Wong, Jul 6, 2006
    #2
    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. Kelsang Wangchuk

    System.Timers.Timer vs. System.Threading.Timer

    Kelsang Wangchuk, Jul 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    756
    Kelsang Wangchuk
    Jul 31, 2003
  2. Jason Shohet

    timer to delay execution of event

    Jason Shohet, Dec 20, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    6,763
    Curt_C [MVP]
    Dec 21, 2004
  3. lynology

    Prob with delay timer

    lynology, Aug 2, 2004, in forum: C++
    Replies:
    2
    Views:
    434
    Ralph D. Ungermann
    Aug 2, 2004
  4. =?Utf-8?B?VW1lc2huYXRo?=

    Timer Control Delay

    =?Utf-8?B?VW1lc2huYXRo?=, Sep 26, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    513
    =?Utf-8?B?VW1lc2huYXRo?=
    Oct 16, 2006
  5. lynology

    Problem with timer delay in C

    lynology, Aug 2, 2004, in forum: C Programming
    Replies:
    1
    Views:
    444
    Mark A. Odell
    Aug 2, 2004
Loading...

Share This Page