Timer with variable delay

  • Thread starter crash.test.dummy
  • Start date
C

crash.test.dummy

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;
}
}
 
O

Oliver Wong

crash.test.dummy said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top