Home > other >  How to terminate timer after a day automatically?
How to terminate timer after a day automatically?

Time:06-19

I am trying to build multiple Timers and scheduled independent tasks for each of them. I have a constructor for holding a Timer and its variable. And I would time to set different lifespan for each timer, for example one timer will be terminated after 1 day whilst keep other run forever. I have a thought to build another a timer on top of those timers and control their status but I am afraid that the program would be messy and bulky. Is there any other method for this? Thanks

Code:

public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        TimerTrigger.INSTANCE.runTimer();
    }
}

To Trigger the timer:

public enum TimerTrigger {
    INSTANCE;

    private TimerTrigger(){}

    public void runTimer(){
      for (int i = 0; i < 3; i  ) {
        System.out.println( "Initiaizing Timer "   i );
        TimerConstructor tmpTimer = new TimerConstructor();
        varObjectvo timerVariable = new varObjectvo();
        timerVariable.setLifespan('24'); //24 hour
        tmpTimer.start(timerVariable); //timerVariable is a value object
      }
    }
}

The Constructor of timer:

import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;
public class TimerConstructor{
    private static varObjectvo timerVO = null;
    Timer timer = null; 
  
    public void start(varObjectvo obj) {
      timer = new Timer("Timer_"   obj.getIndex()); // will be Timer_1/2/3
      timerVO = obj;
      TimerChecker task = new TimerChecker();

      ********** Do Something here to kill timer after 24 hours **********

      timer.scheduleAtFixedRate(task, new Date(), 10000);
    }
  
    private class TimerChecker extends TimerTask {
      public void run() {
        System.out.println("It is timer "   timerVO.getIndex()   " from: "   timer.toString());
      }
    }
  }

The value object class:

public class varObjectvo{
    private Integer lifespan;
    
    public void setLifespan(Integer i){ 
        this.lifespan= i;
    };

    public Integer getLifespan(){ 
        return this.lifespan;
    };
  }

CodePudding user response:

Executors framework

I am trying to build multiple Timers

If you read the Javadoc for Timer & TimerTask, you will see notes explaining that these classes are now legacy.

Is there any other method for this?

Yes.

The Timer & TimerTask classes were long ago supplanted by the Executors framework added to Java 5. See tutorial by Oracle.

Define your task as a Runnable or a Callable. Submit an instance of your task to an ExecutorService.

scheduled independent tasks for each of them.

One executor service can handle multiple tasks.

A scheduled executor service can run a task after a specified amount of time, or run repeatedly.

keep other run forever

Submit your task to a scheduled executor service to have it run repeatedly and indefinitely.

one timer will be terminated after 1 day

Make your task reschedule itself until expiration.

Write your task class such that it holds a reference to the scheduled executor service, remembers when it first started, and reschedules itself for the next run unless the specified limit of time since starting has been reached.

I have a thought to build another a timer on top of those timers

No need. You would be reinventing the scheduled executor service.

Search Stack Overflow to learn more. All of this has been covered multiple times already.

CodePudding user response:

Use Quartz Enterprise Job Scheduler

  • Related