Temporizador/ Thread para aguardar um tempo

Implementando uma classe usando WAIT e o SLEEP

public class MinhaClasse implements Runnable
    private boolean executando;  

    public MinhaClasse() {
        executando = true;
        new Thread(this).start();
    }  

    public void run() {
        while (executando) {
            try {
                synchronized(this) {
                    wait(10000L); // Aguarda 10 segundos
                }
                // ou seria melhor utilizar Thread.sleep()
                Thread.sleep(10000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }
    }
}

Esperar até que outro Thread chame o notify():

wait(long)

Suspender por um certo tempo:

sleep(long)

Thread.sleep(10000L);


  
No comments yet.