Button locked in while

1

My program has a loop, created with the command while . I start the loop with the start button.

However, I can not do anything else in the program while while is running.

I wanted to put a button called Stop that pressed on it, it stopped the program, can do?

Code Ex:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    boolean start = true;
    while(start == true){
        System.out.print("Olá");
    }
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    start= false;
}

Below the image of how the button stays until the loop is finished:

    
asked by anonymous 05.05.2017 / 04:14

2 answers

2

You have to understand the concept of the thread. AWT and Swing run on a thread called EDT (see in this answer as well as in this question ). The EDT is responsible for receiving events from the operating system (mouse, keyboard, drag windows, maximize, minimize, redraw the screen, etc.). EDT is also the thread that responds to events, such as a click of a button.

It's important for the application to be responsive, never overloading the WBS with heavy tasks. You should always make the best effort so that she is free to do what is her purpose and nothing more. That is, tasks such as reading or writing files, doing complicated loop operations, performing batch operations, uploading or downloading data on the internet, etc. from within EDT is a bad idea.

What happens is that you are locking the EDT in an infinite loop. That being said, it will get stuck in the loop and will not be able to process events to redraw the screen or receive the click on the other button. The solution in your case is to use another thread to release the EDT:

private volatile Thread runner;

private void loop() {
    while (runner == Thread.currentThread()) {
        System.out.print("Olá");
    }
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    if (runner != null) return;
    runner = new Thread(this::loop);
    runner.start();
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    runner = null;
}
    
05.05.2017 / 05:03
0

Friend, not a good loops delayed or infinite than this thread of jbutton . Try something like this, in a separate one, to synchronize variables between them is peculiar:

Thread t1 = new Thread(new Runnable() {
    public void run()
    {
        // Código aqui
    }
});  
t1.start();
    
05.05.2017 / 04:57