0

I create a thread in Java inside a button to print a simple message but I cannot stop it.

Thread a = new Thread();
a.start();

while(true){
    try{
        Thread.sleep(2000);
        System.out.println("code");
    }catch(Exception e){
    }
}

when I click on it, itvstarts to print the code, but it seems to be blocked (the button). I would like to know. how can I stop the thread? And if I stop it, would be the button available again?.

I´m using netbeans 7.3, thanks.

Perception
  • 79,279
  • 19
  • 185
  • 195

4 Answers4

1
while(true){
}

starts an infinite loop due to which all the other operations are blocked.

Remove that

Code2Interface
  • 495
  • 2
  • 7
0

The thread you are starting is not doing anything. It starts when you call a.start() and instantly terminates, because there is no code for this thread to run. Following this, the same thread that started the new one, and that is processing the click event, enters an infinite loop, so your user interface is completely blocked.

You need to give some code for the new thread to execute. To do so, you either pass the thread a Runnable or you override the thread's run() method. For example, to give it a Runnable containing the loop that prints every 2 seconds, you could do:

final Thread a = new Thread(new Runnable() {
  @Override public void run() {
    while (true) {
      try {
        Thread.sleep(2000);
        System.out.println("code");
      } catch (InterruptedException e) {
        break;
      }
    }
  }
};
a.start();

After that, if you ever want to stop that thread, you'd need to save a reference to the thread a in a field or something, and then call a.interrupt(). This will cause sleep to throw an InterruptedException, which will be caught and will execute break, which terminates the infinite loop and allows the thread to reach the end of the run method, which terminates the thread.

For example:

private Thread a = null;
... click handler on start button ... {
  if (a == null) {
    a = new Thread(new Runnable() {
      @Override public void run() {
        while (true) {
          try {
            Thread.sleep(2000);
            System.out.println("code");
          } catch (InterruptedException e) {
            break;
          }
        }
      }
    };
    a.start();
  }
}

... click handler on "stop" button ... {
  if (a != null) {
    a.interrupt();
    a = null;
  }
}
Bruno Reis
  • 37,201
  • 11
  • 119
  • 156
0

You do not stop a thread in Java, you send an interrupt() signal.

The Thread may, or may no catch the signal. If it is waiting, or sleeping or joining (wait(), sleep() or join()) has been called on it), an InterruptedException will be raised.

The Thread (in its while loop) can test whether it has been interrupted by calling the isInterrupted() method and then decide to commit suicide (e.g. exit the loop).

Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
0

Use interrupt(). Then handle the InterruptedException

Bizmarck
  • 2,663
  • 2
  • 33
  • 48