[Java] How to Schedule a task to run in an interval

There is often need in application to run some particular task in background to accomplish some work in an interval. The example can be, service running in background for cleanup of application just like, we have the Java Garbage collection.

In this article, i will show you 3 different ways to achieve this

They are as follows

  • using simple thread
  • using TimerTask
  • using ScheduledExecutorService

 

using simple thread

This is very simple, which creates the simple thread puts it run in forever with use of while loop and makes use of sleep method to put the interval between running.

This is simply fast and quick way to achieve it

Following is code for this.

 

using the Timer and TimerTask

Previous method we saw was very quickest possible, but it lacks some functionality

This has much more benefits than previous they are as follows

  • control over when start and cancel task
  • first execution can be delayed if wanted, provides useful

In this we use, Timer class for scheduling purpose and TimerTask is used for enclosing task to be executed inside its run() method.

Timer instance can be shared to schedule the multiple task and it is thread-safe.

When Timer constructor is called , it creates one thread and this single thread is used any scheduling of task.

For our purpose, we use Timer#scheduleAtFixedRate

Following code shows the use of Timer and TimerTask

These classes are classes existed from the JDK 1.3.

using ScheduledExecutorService

This is introduced in java.util.concurrent from Java SE 5 as Concurrency utilities. This is preferred way to achieve the goal.

It provides following benefits as compared to previous solutions

  • pool of threads is used to execute as compared TImer`s single thread
  • Provides the flexibility for delaying first execution
  • Provides nice conventions for providing the time intervals

Following code shows use of same,

 In this, we use ScheduledExecutorService#scheduleAtFixedRate as shown , it takes param as runnable which particular piece of code we want to run , initialdelay for first execution

About these ads

4 thoughts on “[Java] How to Schedule a task to run in an interval

  1. Pingback: java,timertask | 新热网—关注互联网,打造互联网知识与技术分享的科技博客

  2. Pingback: Java实现定时任务的三种方法 | 鸭嘴兽创意博客

  3. How to run the above code for only particular time period say 30 min. After that it should stop executing?

  4. Use ScheduleExecutorService.schedule( Runnable-Instance, intial delay, time unit)

    For e.g

    SES. schedule( your runnable insance, 30, TimeUnit.MINUTES)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s