Close

Java - java.util.Timer Examples

Java 

One time task execution example, using schedule(TimerTask task, long delay)

public class OneTimeTask {

public static void main (String[] args) {


System.out.println("Main thread: " + Thread.currentThread());
Timer timer = new Timer();
final long start = System.currentTimeMillis();
timer.schedule(new TimerTask() {
@Override
public void run () {
System.out.print("Task invoked: " +
(System.currentTimeMillis() - start) + " ms");
System.out.println(" - " + Thread.currentThread());
timer.cancel();
}
}, 1000);

}
}
Original Post




Fixed rate task example , using schedule(TimerTask task, long delay, long period)

public class FixedDelayTask {

private static int count;

public static void main (String[] args) {

System.out.println("Main thread: " + Thread.currentThread());
Timer timer = new Timer();
final long start = System.currentTimeMillis();
timer.schedule(new TimerTask() {
@Override
public void run () {

System.out.print("Task invoked: - " + (++count) + " - " +
(System.currentTimeMillis()
- start) + " ms");
System.out.println(" - " + Thread.currentThread());
}
}, 1000, 500);
}
}
Original Post




Fixed Rate task example using method, scheduleAtFixedRate(TimerTask task, long delay, long period)

public class FixedRateTask {
private static int count;

public static void main (String[] args) {

System.out.println("Main thread: " + Thread.currentThread());
Timer timer = new Timer();
final long start = System.currentTimeMillis();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run () {
System.out.print("Task invoked - " + (++count) + " - " +
(System.currentTimeMillis()
- start) + " ms");
System.out.println(" - " + Thread.currentThread());
}
}, 1000, 500);

}
}
Original Post




Using schedule(TimerTask task, long delay), submitting multiple tasks.

public class OneTimeMultipleTasks {
public static void main (String[] args) {

System.out.println("Main thread: " + Thread.currentThread());
Timer timer = new Timer();
final long start = System.currentTimeMillis();

final TimerTask timerTask1 = new TimerTask() {
@Override
public void run () {
System.out.print("Task1 invoked: " +
(System.currentTimeMillis()
- start) + " ms");
System.out.println(" - " + Thread.currentThread());

try {
/*The task should finish quickly, otherwise it will delay other tasks, here
we are simulating a long running task to see the issue, if you remove this
the second task will run at its provided initial delay*/
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}

/*don't call cancel here cause it will
terminate timer and second task won't run.*/
//timer.cancel();

}
};

final TimerTask timerTask2 = new TimerTask() {
@Override
public void run () {
System.out.print("Task2 invoked : " +
(System.currentTimeMillis()
- start) + " ms");
System.out.println(" - " + Thread.currentThread());
timer.cancel();
}
};

timer.schedule(timerTask1, 1000);

/*
second submission of the same task will cause
java.lang.IllegalStateException: Task already scheduled or cancelled
timer.schedule(timerTask1, 2000);
*/

timer.schedule(timerTask2, 2000);
}
}
Original Post




See Also