How to use the TIMER class in a method with Math.max and Math.min

2

I have an ambitious project to create my math game (which I found to be better than a simple calculator) in just a JFrame , with methods only, without using any other class in the package ...

With the help of Swing and Netbeans builder GUI

One day, I've heard of the Timer class, however, I can not find anything about using it within a method, just inside another class without main

I tried to use it in a method in a question I asked at a certain time, unfortunately, I ended up losing the code (because it did not work)

Could someone give me an example of what it would be like to use this class in a method? (preferably in a decreasing timer)

    
asked by anonymous 15.04.2016 / 20:09

1 answer

1

You can use TimerTask and Timer of java.util Here's an example:

import java.util.TimerTask;

public class ExemploTimer  extends TimerTask {

    private int numeroExecucao = 0;

    @Override
    public void run() {
        numeroExecucao++;
        System.out.println("Exemplo Timer executando: " + numeroExecucao);
        //Aqui você executa as suas ações da thead.
    }

}

RUNNING YOUR CLASS

public class ExemploMain {
    public static void main(String args[]){
        //Em qualquer lugar você pode utilizar a sua classe.

        ExemploTimer exemploTimer = new ExemploTimer();


        Timer timer = new Timer(true);
        //data atual + 10 minutos.
        Date dataPrimeiraExecucao = new Date();
        //Tempo para execução, executar a cada 10 segundos
        long tempoParaProximaExecucao = 1000 * 10; 
        // Shedule, acredito que isto é o que você precisa
        timer.schedule(exemploTimer, dataPrimeiraExecucao, tempoParaProximaExecucao);

        //Este código serve apenas para esperar a linha acima executar.
        //Deve ser eliminado no seu código.
        try {
            Thread.sleep(1000 * 100);
        } catch (InterruptedException ex) {
            Logger.getLogger(ExemploMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

To complement some examples and tutorials:

Timer

Timer Task

    
15.04.2016 / 23:33