-6

I want to know when my thread dies and, after this, execute some code.

I'm using a while but my program is showing a bad execution.

The command while is holding the program.

Here is my code:

public class Thread_para_Menu extends Thread {        
    public void run() {
        System.out.println("executando thread");
        for (int i = 0; i < 100; i++) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException ex) {
                Logger.getLogger(Teste_Inicio.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (i % 2 == 0) {
                jlblAguarde.setForeground(Color.yellow);
            } else {
                jlblAguarde.setForeground(Color.orange);
            }
        }
        System.out.println("FIM DO RUN");
    }
}

public void Pisca_Menu () {
    int xx =0;
    Thread_para_Menu TEMPO_MENU = new Thread_para_Menu();
    TEMPO_MENU.start();
    while (TEMPO_MENU.isAlive()){
        System.out.println("THREAD VIVE");
    }
    System.out.println("THREAD MORREU");
}
Didier L
  • 18,905
  • 10
  • 61
  • 103
Rodrigo
  • 1
  • 1
  • 3
    Possible duplicate of [Java Wait for thread to finish](https://stackoverflow.com/questions/4691533/java-wait-for-thread-to-finish) – Tom Dec 19 '17 at 13:52
  • Re, "...but my program is showing a bad execution." If you want help understanding an error message, then you should include the message in your question. – Solomon Slow Dec 19 '17 at 14:48
  • thanks for your tip. – Rodrigo Dec 20 '17 at 13:52

2 Answers2

0

TEMPO_MENU.join();

Please note that (a) this throws InterruptedException, which you need to cater for, and (b) this is in the JavaDocs at https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html, where it literally says "Waits for this thread to die."

SeverityOne
  • 2,476
  • 12
  • 25
0

I get solve my problem.

I make a thread with FOR. When the count arrive the end, i execute my code.

Thanks all!!!!

Rodrigo
  • 1
  • 1