To schedule recurring tasks in a Java application

Source: Internet
Author: User

All types of Java applications typically need to plan for recurring tasks. Enterprise applications need to schedule daily logs or nightly batch processes. A J2SE or J2ME calendar application needs to schedule the alarm time according to the user's agreement. However, the standard scheduling class Timer and timertask do not have enough flexibility to support the type of scheduled tasks that are typically required. In this article, Java developer Tom White shows you how to build a simple, generic planning framework for performing arbitrarily complex scheduling tasks.

I'll collectively refer to Java.util.Timer and Java.util.TimerTask as Java timer frameworks, which make it easy for programmers to plan simple tasks (note that these classes can also be used in J2ME). Before this framework was introduced in the Java 2 SDK, Standard Edition, Version 1.3, developers had to write their own scheduler, which took a lot of effort to process threads and complex object.wait () methods. However, the Java timer framework does not have sufficient capacity to meet the scheduling requirements of many applications. Even a task that needs to be repeated at the same time of day cannot be planned directly by using a Timer, because time jumps occur at the beginning and end of daylight savings.

This article presents a common Timer and timertask planning framework that allows for more flexible scheduling tasks. The framework is simple-it includes two classes and an interface-and is easy to master. If you are accustomed to using the Java Timer Framework, you should be able to quickly grasp the planning framework (see Resources for more information on the Java Timer Framework).

Schedule a single task

The planning framework is based on the Java timer framework class. So before we explain how to use the planning framework and how to implement it, we'll look first at how to plan with these classes.

Imagine a boiled egg timer that will make a sound to remind you after a few minutes (when the egg is cooked). The code in Listing 1 forms the basic structure of a simple boiled egg timer, written in the Java language:

Listing 1. Eggtimer class

package org.tiling.scheduling.examples;
import java.util.Timer;
import java.util.TimerTask;
public class EggTimer {
   private final Timer timer = new Timer();
   private final int minutes;
   public EggTimer(int minutes) {
     this.minutes = minutes;
   }
   public void start() {
     timer.schedule(new TimerTask() {
       public void run() {
         playSound();
         timer.cancel();
       }
       private void playSound() {
         System.out.println("Your egg is ready!");
         // Start a new thread to play a sound...
       }
     }, minutes * 60 * 1000);
   }
   public static void main(String[] args) {
     EggTimer eggTimer = new EggTimer(2);
     eggTimer.start();
   }
}

The Eggtimer instance has a Timer instance that provides the necessary planning. After starting the boiled egg timer with the start () method, it plans a timertask that is executed after the specified number of minutes. When the time is up, the Timer calls the TimerTask start () method in the background, which makes it sound. The application will abort after the timer is canceled.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.