Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java timer task schedule

From reading on Stack Overflow I've seen that many of you don't recommend using Timer Task. Hmmm... but I already implemented this:

I have this code:

detectionHandlerTimer.schedule(myTimerTask, 60 * 1000, 60 * 1000);

The thing is that work of myTimerTask lasts some time.

I would like this behavior:

  1. wait 60 sec.
  2. do task for some time (e.g. 40 - 100 sec).
  3. task finished.
  4. wait 60 seconds.
  5. do task for some time (e.g. 40 - 100 sec).

But the code above behaves like this

  1. wait 60 sec.
  2. do task for some time (e.g. 40 - 100 sec).
  3. task finished
  4. do task for some time (e.g. 40 - 100 sec).

Because the time duration of task is bigger than 60, timer starts task immediately after task is finished. But I would like it to wait again.

like image 347
vale4674 Avatar asked Oct 28 '10 15:10

vale4674


People also ask

Can we set Timer in Java?

Java programming language provides a class utility known as Timer Task. It allows one to schedule different tasks. In other words, a task can be executed after a given period or at a specified date and time. A Timer in Java is a process that enables threads to schedule tasks for later execution.

What does Timer task do in Java?

TimerTask is an abstract class defined in java. util package. TimerTask class defines a task that can be scheduled to run for just once or for repeated number of time. In order to define a TimerTask object, this class needs to be implemented and the run method need to be overridden.

What does Timer schedule do?

The schedule(TimerTask task,long delay,long period) method is used to schedule the specified task for repeated fixed-delay execution, beginning after the specified delay.

How do you repeat a Timer in Java?

Use timer. scheduleAtFixedRate() to schedule it to recur every two seconds: Schedules the specified task for repeated fixed-rate execution, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.


1 Answers

This works. The key is to have the task itself (after it completes) schedule the next occurrence of the task.

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

public class TaskManager {

    private Timer timer = new Timer();

    public static void main(String[] args) {
        TaskManager manager = new TaskManager();
        manager.startTask();
    }

    public void startTask() {
        timer.schedule(new PeriodicTask(), 0);
    }

    private class PeriodicTask extends TimerTask {
        @Override
        public void run() {
            System.out.println(System.currentTimeMillis() + " Running");

            /* replace with the actual task */
            try {
                Thread.sleep(15 * 1000);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
            /* end task processing */

            System.out.println(System.currentTimeMillis() + " Scheduling 10 seconds from now");
            timer.schedule(new PeriodicTask(), 10 * 1000);
        }
    }
}

Which prints:

$ javac TaskManager.java && java TaskManager
1288282514688 Running
1288282529711 Scheduling 10 seconds from now
1288282539712 Running
1288282554713 Scheduling 10 seconds from now
1288282564714 Running

Here's what it looks like if you extract the second components of the timestamps (for clarity):

$ javac TaskManager.java && java TaskManager
14                                    Running
29 (+15 seconds execution)            Scheduling 10 seconds from now
39 (+10 seconds delay until next run) Running
54 (+15 seconds execution)            Scheduling 10 seconds from now
64 (+10 seconds delay until next run) Running

Just replace the 10s with 60s.

like image 157
Rob Hruska Avatar answered Oct 08 '22 03:10

Rob Hruska