Break loop in java

3

My Java project is a headlamp control with Arduino. So far so good. However I did a function in which the leds gets automatic in a loop after a RadionButton is selected. My problem is that when I select RadionButton my JFrame freezes and will not let me deselect RadionButton .

In other words, selecting the button causes an active loop. The idea would be to uncheck the button, make the loop stop.

Follow my code.

Here is where the button will be selected and will send the command to the function:

        if ( teste.isSelected() ) {
        try {
            utiArduino.enviarDados( "automatico" );
        } catch (InterruptedException | IOException ex) {
            Logger.getLogger(Jframe_farol.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        try {
            utiArduino.enviarDados( "DesligaAuto" );
        } catch (InterruptedException | IOException ex) {
            Logger.getLogger(Jframe_farol.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Here is the function of the loop:

  public void enviarDados(String status) throws InterruptedException, IOException{
   int i = 8;

    if ( "automatico".equals(status) ) { // caso do automatico   
        while ( true ) {
            output.write(i);
            Thread.sleep(1000);
            i++;
            if ( i > 10 ){
                i = 8;
            }
        }
    }
} 
    
asked by anonymous 18.11.2016 / 07:39

1 answer

2

AWT and Swing have a thread that manages their operation, it is called Event Dispatch Thread - EDT. Since it is a single thread, if you drop it in while (true) and / or Thread.sleep(1000) , the graphical interface will be frozen.

So the solution is to move these things to a different thread. Try to do so in your class utiArduino :

private volatile String ultimoStatus;
private volatile Thread threadArduino;

public void enviarDados(String status) {
   ultimoStatus = status;
   if (threadArduino == null) {
       threadArduino = new Thread(this::iniciarEnvioArduino);
       threadArduino.start();
   }
}

private void envioArduino() {
    int i = 8;
    try {
        while (Thread.currentThread() == threadArduino) {
            if ("automatico".equals(ultimoStatus)) { // caso do automatico   
                output.write(i);
                Thread.sleep(1000);
                i++;
                if (i > 10) i = 8;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        // Não faz nada e deixa a thread morrer.
    }
}

public void parar() {
    if (threadArduino != null) {
        Thread t = threadArduino;
        threadArduino = null;
        t.interrupt();
    }
}

The selection of your button looks like this:

utiArduino.enviarDados(teste.isSelected() ? "automatico" : "DesligaAuto");

In your subclass of JFrame (assuming you have the reference utiArduino ), put this too:

@Override
public void dispose() {
    super.dispose();
    utiArduino.parar();
}

For more information on EDT, please read my other answer as well.

    
18.11.2016 / 11:23