Phanimadhavi Vasantala
Phanimadhavi Vasantala

Reputation: 225

Java Timer Task

I wanted to know if the below code will run infinitely until the server is stopped?

public class Class1()
 {
   public static void main(String args[])
      {
            TimerTask task = new CacheThread();

            Timer timer = new Timer();
            timer.schedule(task, 1000,10000);
      }
  }

Here im calling the task which is in the below class:

     package com.verizon.esupport.cache;

     import java.sql.Timestamp;
     import java.util.Date;
     import java.util.HashMap;
     import java.util.TimerTask;


     public class CacheThread extends TimerTask {//implements java.io.Serializable{


 RetrieveHdrFtrContent rhf = RetrieveHdrFtrContent.getSingleInstance();
 HeaderBean hdrbn = HeaderBean.getSingleInstance();

 String header = "";
 String footer= "";
 String sHeader = "";

@Override
public void run() {

    try{
    // TODO Auto-generated method stub
     HashMap headerm = new HashMap();
     HashMap smallHeaderm = new HashMap();
     HashMap footerMapm = new HashMap();

     Date date = new Date();

     header = rhf.headerContent().toString();
    //  headerm.clear();
    //  hdrbn.setLheaderMap(headerm);
        headerm.put("header", header);
        headerm.put("timeStamp", new Timestamp(date.getTime()));
        hdrbn.setLheaderMap(headerm);

        footer = rhf.footerContent().toString();
        footerMapm.clear();
        hdrbn.setLfooterMap(footerMapm);
        footerMapm.put("footer", footer);
        footerMapm.put("timeStamp", new Timestamp(date.getTime()));
        hdrbn.setLfooterMap(footerMapm);

        sHeader = rhf.smallHeader().toString();
        smallHeaderm.clear();
        hdrbn.setLsmallHeaderMap(smallHeaderm);
        smallHeaderm.put("header", sHeader);
        smallHeaderm.put("timeStamp", new Timestamp(date.getTime()));
        hdrbn.setLsmallHeaderMap(smallHeaderm);

        System.out.println("raising exception :: "+(1/0));

    }
    catch(Exception e)
    {
        e.printStackTrace();
        System.out.println("pls continue the task");
    }
}

}

Will this task execute till the server is stopped? Also let me know if any exception occurs in one task, will the executor stop or will it continue executing the nex tasks subsequently? Can someone please answer..

Upvotes: 2

Views: 2129

Answers (2)

bennihepp
bennihepp

Reputation: 262

If an exception is thrown within the run() method the timer will stop executing your task. You have to catch exceptions in your run() method as you are doing or use a ScheduledExecutorService instead.

To stop your application you will have to explicitely stop the thread (or in this case cancel the timer) as it is not a daemon thread.

Edited

Example of ScheduledExecutorService copied from JavaDoc (http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html):

import static java.util.concurrent.TimeUnit.*;
 class BeeperControl {
    private final ScheduledExecutorService scheduler = 
       Executors.newScheduledThreadPool(1);

    public void beepForAnHour() {
        final Runnable beeper = new Runnable() {
                public void run() { System.out.println("beep"); }
            };
        final ScheduledFuture<?> beeperHandle = 
            scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
        scheduler.schedule(new Runnable() {
                public void run() { beeperHandle.cancel(true); }
            }, 60 * 60, SECONDS);
    }
 }

Upvotes: 1

John Smith
John Smith

Reputation: 2332

It will run forever. Since you catch all exceptions, every task run finishes successfully.

This of course does not guarantee, that the program will work correctly infinitely since what you do with your RetrieveHdrFtrContent may have sideeffects (like memory leaks for example).

Upvotes: 0

Related Questions