This Java code illustrates the use of two timers running at the same time using java.util.Timer and java.util.TimerTask.
TimerExample.java starting two timers:
import java.util.Timer;
/**
* An example on multiple timers.
*
* @author http://www.gammelsaeter.com/
*/
public class TimerExample {
public static void main(String[] args) {
Timer timer1 = new Timer(); // Get timer 1
Timer timer2 = new Timer(); // get timer 2
long delay1 = 5*1000; // 5 seconds delay
long delay2 = 3*1000; // 3 seconds delay
// Schedule the two timers to run with different delays.
timer1.schedule(new Task("object1"), 0, delay1);
timer2.schedule(new Task("Object2"), 0, delay2);
}
}
/**
* An example on multiple timers.
*
* @author http://www.gammelsaeter.com/
*/
public class TimerExample {
public static void main(String[] args) {
Timer timer1 = new Timer(); // Get timer 1
Timer timer2 = new Timer(); // get timer 2
long delay1 = 5*1000; // 5 seconds delay
long delay2 = 3*1000; // 3 seconds delay
// Schedule the two timers to run with different delays.
timer1.schedule(new Task("object1"), 0, delay1);
timer2.schedule(new Task("Object2"), 0, delay2);
}
}
TimerTask.java which is the task that is run by the two timers in Timer.java:
import java.util.TimerTask;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
* This is a timertask because it extends the class java.util.TimerTask. This class
* will be given to the timer (java.util.Timer) as the code to be executed.
*
* @see java.util.Timer
* @see java.util.TimerTask
* @author http://www.gammelsaeter.com/
*/
public class Task extends TimerTask {
private String _objectName; // A string to output
/**
* Constructs the object, sets the string to be output in function run()
* @param str
*/
Task(String objectName) {
this._objectName = objectName;
}
/**
* When the timer executes, this code is run.
*/
public void run() {
// Get current date/time and format it for output
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss");
String current_time = format.format(date);
// Output to user the name of the objecet and the current time
System.out.println(_objectName + " - Current time: " + current_time);
}
}
import java.util.Date;
import java.text.SimpleDateFormat;
/**
* This is a timertask because it extends the class java.util.TimerTask. This class
* will be given to the timer (java.util.Timer) as the code to be executed.
*
* @see java.util.Timer
* @see java.util.TimerTask
* @author http://www.gammelsaeter.com/
*/
public class Task extends TimerTask {
private String _objectName; // A string to output
/**
* Constructs the object, sets the string to be output in function run()
* @param str
*/
Task(String objectName) {
this._objectName = objectName;
}
/**
* When the timer executes, this code is run.
*/
public void run() {
// Get current date/time and format it for output
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss");
String current_time = format.format(date);
// Output to user the name of the objecet and the current time
System.out.println(_objectName + " - Current time: " + current_time);
}
}

hi,good afernoon, I have a question need you give me a answer,that it could’t use timer componet in the java web application?
I do not have experience with java web applications, so I can’t give you any answer. You can try asking at Sitepoint forums. I use that forum a lot, it’s pretty good.
I changed the ‘mm’ in the date format to ‘MM’ to show the month otherwise its the same as ‘mm’ in time and will show minutes. Don’t know if its works in other compilers.
You are right blia, that was an error. The post is updated!
[...] Originally Posted by ch41 am newbie and i have problem to write a scheduling script in java, this script should do something in certain time interval… Could anybody help me, maybe give some example or source that maybe useful… I really thnx for any answer… Spell out your words and write clearly. And yes, we can help you, but we're not going to write it FOR you. Post what you've written/tried so far, and where you're stuck, and we can assist. Otherwise, there are many Java timer examples you can easily find if you tried to look. First hit in Google: http://www.gammelsaeter.com/programm…ample-in-java/ [...]
@lixingxing : Vijay Singh :
yes it can be used with web application j2ee i m creating it instance in ServletContextListener so tht the timer can be started whn the web app is first loaded into the container hope this will help u ..