Use Timer to schedule periodic tasks

The java.util.Timer and java.util.TimerTask classes can be used for many simple thread related tasks. Use them to perform an action either periodically or just once. As well, the first execution can be delayed to a specific time in the future.

The general guideline is :

Example

Here, a task is performed once a day at 4 a.m., starting tomorrow morning.


import java.util.Timer;
import java.util.TimerTask;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;

public final class FetchMail extends TimerTask {

  /**
  * Construct and use a TimerTask and Timer.
  */
  public static void main (String... arguments ) {
    TimerTask fetchMail  = new FetchMail();

    //perform the task once a day at 4 a.m., starting tomorrow morning
    //(other styles are possible as well)
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(fetchMail, getTomorrowMorning4am(), fONCE_PER_DAY);
  }

  /**
  * Implements TimerTask's abstract run method.
  */
  public void run(){
    //toy implementation
    System.out.println("Fetching mail...");
  }

  // PRIVATE ////

  //expressed in milliseconds
  private final static long fONCE_PER_DAY = 1000*60*60*24;

  private final static int fONE_DAY = 1;
  private final static int fFOUR_AM = 4;
  private final static int fZERO_MINUTES = 0;

  private static Date getTomorrowMorning4am(){
    Calendar tomorrow = new GregorianCalendar();
    tomorrow.add(Calendar.DATE, fONE_DAY);
    Calendar result = new GregorianCalendar(
      tomorrow.get(Calendar.YEAR),
      tomorrow.get(Calendar.MONTH),
      tomorrow.get(Calendar.DATE),
      fFOUR_AM,
      fZERO_MINUTES
    );
    return result.getTime();
  }
}
 



See Also :
Timers
Would you use this technique?
Yes   No   Undecided   
© 2010 Hirondelle Systems | Source Code | Contact | License | Quotes | RSS
Individual code snippets can be used under this BSD license - Last updated on January 4, 2009.
Over 100,000 unique IPs last month - Built with WEB4J.
- In Memoriam : Bill Dirani -