Schedule Tasks with Timer

Asked

Viewed 1,060 times

3

Situation:


Description: I have this Timer to perform my tasks in a certain period of time according to the seconds informed.


Problem: When running the program the tasks starts by ignoring the seconds reported, after the task is executed for the first time the time is respected.


Need: When the program is started it should wait the seconds that informed "30", "60", "1200" and then the task should begin.


    public executarTarefas(int seconds) {
            timer = new Timer();
            timer.schedule(new limparQuarto(), Calendar.getInstance().getTime(), seconds * 1000);   
}

Observing: This method performs the task limparQuarto() which is only an example, if someone knows how to reach "need" misfortune.

  • 1

    Dear Eduardo, did my statement solve your problem? If yes, would you kindly mark the question as answered, to assist in the organization?

1 answer

4


The second parameter of schedule refers to the first execution, therefore it should be the informed period plus the sum of the second desired, as referenced below:

http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html

public void schedule(TimerTask task,
            Date firstTime,
            long period) 

Schedules the specified task for repeated Fixed-delay Execution, Beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.

In Fixed-delay Execution, each Execution is scheduled relative to the current Execution time of the Previous Execution. If an Execution is delayed for any Reason (such as Garbage Collection or other background Activity), subsequent executions will be delayed as well. In the long run, the Frequency of Execution will generally be Slightly Lower than the reciprocal of the specified period (assuming the system clock underlying Object.Wait(long) is Accurate). As a consequence of the above, if the scheduled first time is in the Past, it is scheduled for immediate Execution.

Fixed-delay Execution is appropriate for recurring activities that require "smoothness." In other words, it is appropriate for activities Where it is more Important to Keep the Frequency Accurate in the short run than in the long run. This includes Most Animation tasks, such as blinking a cursor at regular intervals. It also includes tasks wherein regular Activity is performed in Response to Human input, such as Automatically Repeating to Character as long as a key is Held down.

Parameters:

task - task to be scheduled.

firsttime - First time at which task is to be executed.

period - time in milliseconds between successive task executions.

Throws: Illegalargumentexception - if firsttime.getTime() < 0, or period <= 0

Illegalstateexception - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.

Nullpointerexception - if task or firsttime is null

Browser other questions tagged

You are not signed in. Login or sign up in order to post.