The Java Timer Class is a powerful tool that allows you to schedule tasks to be executed at specified intervals. Whether you need to run a task once or repeatedly, the Timer Class provides a convenient way to automate your Java programs. In this article, we will explore the features of the Java Timer Class and learn how to implement it effectively.
![Java Timer Class - 1]()
Introduction to the Java Timer Class
The Timer Class is part of the java.util package and is used to schedule tasks for execution at a specified time or at regular intervals. It provides a simple and reliable way to execute code at predetermined intervals, making it ideal for tasks such as triggering events, performing periodic updates, or scheduling background processes. To use the Timer Class, you need to create a Timer object from the java.util.Timer class. This Timer object is responsible for scheduling and executing tasks based on your requirements.Creating a Timer Object
Let's start by creating a Timer object and exploring its methods. In the following code example, we create a Timer object and schedule a task to be executed once after a specified delay:
// First we will import the java.util.Timer & java.util.TimerTask classes
import java.util.Timer;
import java.util.TimerTask;
public class TimerExample {
public static void main(String[] args) {
Timer timer = new Timer(); // Creating a Timer object from the timer class
TimerTask task1 = new TimerTask() {
public void run() {
System.out.println("Task 1 executed!");
}
};
TimerTask task2 = new TimerTask() {
public void run() {
System.out.println("Task 2 executed!");
}
};
TimerTask task3 = new TimerTask() {
public void run() {
System.out.println("Task 3 executed!");
}
};
// Scheduling tasks to run after specified delays
timer.schedule(task1, 2000); // Using the schedule method of the timer class
timer.schedule(task2, 4000); // Using the schedule method of the timer class
timer.schedule(task3, 6000); // Using the schedule method of the timer class
}
}
In this updated example, the Timer object is created using the Timer class. Each TimerTask represents a task to be scheduled and executed by the timer. The run method within each TimerTask contains the code to be executed when the task runs.
The tasks are then scheduled using the schedule method of the Timer class, specifying the task and the delay in milliseconds. In this code, the schedule method is used three times, each time with a different task and delay.
Output
When you run the code, it will output:Task 1 executed!
Task 2 executed!
Task 3 executed!
Each task is executed after its respective delay, demonstrating the functionality of the Timer class.
Timer Methods
The Timer Class provides various methods to schedule tasks for execution. Let's explore some of the commonly used methods:1. schedule(TimerTask task, long delay)
This method schedules the specified task for execution after the specified delay. The delay is specified in milliseconds. For example:
timer.schedule(task, 5000); // Schedule the task to run after a 5-second delay
2. schedule(TimerTask task, Date time)
This method schedules the specified task for execution at the specified time. The task will be executed only once. For example:
Date executionTime = new Date(System.currentTimeMillis() + 5000); // Get the current time + 5 seconds
timer.schedule(task, executionTime); // Schedule the task to run at the specified time
3. schedule(TimerTask task, long delay, long period)
This method schedules the specified task for repeated execution after the specified delay and at the specified intervals. The delay is the time in milliseconds before the first execution, and the period is the time in milliseconds between subsequent executions. For example:
timer.schedule(task, 2000, 5000); // Schedule the task to run after a 2-second delay and repeat every 5 seconds
Java Timer Schedule
In addition to the basic scheduling methods, the Timer Class also provides a powerful scheduling method called schedule(TimerTask task, Date firstTime, long period). This method allows you to schedule a task for repeated execution starting at a specific time and at regular intervals. Here's an example that demonstrates the usage of this method:
import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;
public class TimerExample {
public static void main(String[] args) {
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
System.out.println("Task executed!");
}
};
// Schedule the task to run every 5 seconds starting from the current time
Date startTime = new Date();
timer.schedule(task, startTime, 5000);
}
}
In this example, we create a Timer object and define a TimerTask as before. We also create a startTime object of type Date to specify the starting time for the task. The schedule method is then used to schedule the task to run every 5 seconds starting from the current time.
With this scheduling method, the task will continue to execute at the specified interval until the Timer object is canceled or the program is terminated. Otherwise, it will throw an exception.
GO TO FULL VERSION