dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tip: Automation of Tasks

Learn how to create a task and schedule it at your convenience to execute the required actions.


advertisement

Automation of tasks is a good concept. Consider the example below in which you create a task and schedule it at your convenience to execute the needed actions. There are other types of scheduling as well. Explore them to know more.

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

public class TaskScheduling
{
   public static void main(String args[])
   {
      TaskScheduling taskScheduling = new TaskScheduling();
      taskScheduling.proceed();
   }
   
   private void proceed()
   {
      Timer taskTimer = new Timer();

      TimerTask timerTask = new TimerTask()
      {
         public void run() 
         {
            //Our task action
            System.out.println("In run() method");
         }
      };
      
      //The below setting will ensure that the taskTimer is executed after 5 seconds
      taskTimer.schedule(timerTask,5000);
   }
}

/*

Expected output:

[root@mypc]# java TaskScheduling
In run() method

*/

 

Visit the DevX Tip Bank

 



   
Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Thanks for your registration, follow us on our social networks to keep up-to-date