Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Schedule a task using Java

In this article you will learn how to create a program which can be helpfull to you to run a particular task after a particular interval of time.

This can be helpful in situation where you have certain task which you want to run after a particular delay, like scheduling some operation to run after a defined interval.

Here the main classes are Timer and TimerTask. These both classes ensures the scheduling of the task to run after particular delay.

Now we start with the program. First we will import all the necessary classes.

Listing 1: Importing classes

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

Here,

  1. Timer is used to schedule a task to run after a particular delay

Now we define the class along with the main function

Listing 2: Class definition along with main function

public class ScheduleTask extends TimerTask{
	
	public static void main(String args[])
{
	new ScheduleTask().start();
}
	
 

Here,

  1. We define a class called ScheduleTask and extend the TimerTask class since we need to use the timer to schedule a task
  2. We define a main method within which we call the start method of this class

Now we will define the start method which will contain all the processing. This will be responsible for scheduling the task which will run after few intervals which is 5 seconds.

Listing 3: Defining the start method

public void start()
{
	Timer t =new Timer();
	System.out.println("The task will run after 5 seconds");
	t.schedule(new ScheduleTask(),5000);
}

Here,

  1. We define a timer object inside the start method .
  2. Now we schedule it using the schedule method. Here we pass the object of this class as an argument plus the delay after which this activity will be scheduled.The time given here is in millisecond.So 5000ms=5sec

Now we define the run method which will define the task which will run after a particular delay

Listing 4: Defining the run method

public void run() 
{
	System.out.println("Running job after a delay of 5 seconds");
	System.exit(0);
}
} 

Here,

  1. We define run method which will override the run method in TimerTask
  2. Now we print a message.This message will be printed when this task will run.We make use of System.exit to exit the task once it get printed
  3. The message printed will be Running job after a delay of 5 seconds

Listing 5:Full Source code

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

public class ScheduleTask extends TimerTask{
	
	public static void main(String args[])
{
	new ScheduleTask().start();
}
	
public void start()
{
	Timer t =new Timer();
	System.out.println("The task will run after 5 seconds");
	t.schedule(new ScheduleTask(),5000);
}

public void run() 
{
	System.out.println("Running job after a delay of 5 seconds");
	System.exit(0);
}
}

Output

The task will run after 5 seconds
Running job after a delay of 5 seconds

This is all. Hope you liked the article. See you next time.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login