How to create a timer in JAVA?

3

I'd like to determine how long an image should stay until it comes back to the other. Is there any class for this? Otherwise, how to create?

Note: Preferred is the absence of Threads.

    
asked by anonymous 06.11.2015 / 00:35

1 answer

7

To run commands after a certain time, you can use the java.util.Timer class % .

For example:

new java.util.Timer().schedule(new TimerTask() {
    @Override
    public void run() {
        //executar ação aqui
    }
}, 1000); //executar após 1 segundo

If you prefer a simplified version:

Timer cronometro = new Timer();
TimerTask tarefa = new TimerTask() {
    @Override
    public void run() {
        //executar ação aqui
    }
};
int milissegundos = 1000;
cronometro.schedule(tarefa, milissegundos);

Explanation

Basically, the Timer object allows you to schedule a task to run after a period of time.

So, to define which task we want to execute we need to pass an object that contains the action. In this case, it is an implementation in method run of TimerTask .

Finally, we spend the time in milliseconds, that is, every 1000 units equals one second of waiting.

Repeating with regular intervals

The Timer class also allows you to repeat the task at predefined time intervals, if you add a third parameter with the interval between runs.

Example:

int milissegundos = 1000;
int intervalo = 10000;
cronometro.schedule(tarefa, milissegundos, intervalo);

In this example, the timer waits a second, executes the task, waits 10 seconds, executes the task again, and continues doing so.

The time interval between the end of one run and the beginning of the next run is exactly 10 seconds. So if a task takes 5 seconds, the time between one start and another will be 15 seconds.

Repetition with regular times

The scheduleAtFixRate method allows you to schedule runs at regular times:

int milissegundos = 1000;
int intervalo = 10000;
cronometro.scheduleAtFixRate(tarefa, milissegundos, intervalo);

This means that the first task will run after 1 second and the next 10 seconds after the first one. It does not matter if the first task takes 2 or 5 seconds. This type of timer makes the interval between the beginning of an execution and the beginning of the next one being exactly 10 seconds, regardless of how long a task may take.

Alternative

If you do not worry about using a bit of threads , even though the class ScheduledExecutorService is also interesting.

    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    Runnable tarefa = new Runnable() {
        @Override
        public void run() {
            //executar ação aqui
        }
    };
    scheduler.schedule(tarefa, 1, TimeUnit.DAYS);

You basically instantiate the scheduler and then pass it a task to be executed after a period.

The difference is that you can specify the unit of time, so it is very useful to avoid absurd calculations with milliseconds turning days, months or years.

Like Timer , this class presents methods to run at regular intervals, namely scheduleAtFixedRate and scheduleWithFixedDelay . The names are self-explanatory if you understood the concepts I explained above about Timer .

    
06.11.2015 / 00:51