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 / July 2006

Tip: Looking for answers? Try searching our database.

Timer with variable delay

Thread view: 
crash.test.dummy - 06 Jul 2006 16:30 GMT
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;
  }
}
Oliver Wong - 06 Jul 2006 16:35 GMT
>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


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



©2008 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.