Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Timer with variable delay

Reply
Thread Tools

Timer with variable delay

 
 
crash.test.dummy
Guest
Posts: n/a
 
      07-06-2006
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;
}
}

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      07-06-2006
"crash.test.dummy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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

 
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
Timer Control Delay =?Utf-8?B?VW1lc2huYXRo?= ASP .Net 2 10-16-2006 09:57 AM
Calculating propagation delay & transmission delay Stone Cisco 1 09-27-2006 06:26 PM
timer to delay execution of event Jason Shohet ASP .Net 8 12-21-2004 03:20 PM
Prob with delay timer lynology C++ 2 08-02-2004 05:30 PM
Problem with timer delay in C lynology C Programming 1 08-02-2004 01:13 PM



Advertisments