Home » Core Java » util » Timer » Java Timer and TimerTask Example Tutorial

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Java Timer and TimerTask Example Tutorial

In this tutorial we are going to see how you can use Timer and TimerTask classes of the java.util package in order to schedule the execution of a certain process.

The Timer class uses several flexible methods to make it possible to to schedule a task to be executed at a specific time, for once or for several times with intervals between executions.

To create your own schedulable processes, you have to create your own class the extends TimerTask class. TimerTask implements Runnable interface, so you have to override the run() method.
Let’s see the code :

package com.javacodegeeks.java.core;

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

public class TimerTaskExample extends TimerTask {

	public void run() {
		System.out.println("Start time:" + new Date());
		System.out.println("End time:" + new Date());

	// simulate a time consuming task
	private void doSomeWork() {
		try {


		} catch (InterruptedException e) {

	public static void main(String args[]) {

		TimerTask timerTask = new TimerTaskExample();
		// running timer task as daemon thread
		Timer timer = new Timer(true);
		timer.scheduleAtFixedRate(timerTask, 0, 10 * 1000);
		System.out.println("TimerTask begins! :" + new Date());
		// cancel after sometime
		try {
		} catch (InterruptedException e) {
		System.out.println("TimerTask cancelled! :" + new Date());
		try {
		} catch (InterruptedException e) {



TimerTask begins! :Fri Jan 25 21:36:43 EET 2013
Start time:Fri Jan 25 21:36:43 EET 2013
End time:Fri Jan 25 21:36:53 EET 2013
Start time:Fri Jan 25 21:36:53 EET 2013
TimerTask cancelled! :Fri Jan 25 21:37:03 EET 2013
End time:Fri Jan 25 21:37:03 EET 2013

Don’t forget take a careful look a the documentation of Timer and TimerTask to explore all the features of this mechanism.

This was a Java Timer and TimerTask Example.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....


Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.