1
public class Threadsample implements ActionListener {
HelloRunner hr = new HelloRunner();
Thread tr1 = new Thread(hr, "ThreadOne");
public void actionPerformed(ActionEvent ae)
    {

        Object source = ae.getSource();
        if (source == b2){
            hr.stopRunning();
        }
        if (source== b1){

            tr1.start();
        }
    }


    public class HelloRunner implements Runnable
    {
        private volatile boolean timeToQuit=false;
        int i = 0;
        public void run(){
            while ( ! timeToQuit ){
                  System.Out.Println(i);
                  i++
            }
        }
        public void stopRunning() {
             timeToQuit=true;
             }
    }
}

How do I stop the running thread?

Karlx Swanovski
  • 2,869
  • 9
  • 34
  • 67

4 Answers4

1

Thread interruption is the way to go:

// computingThread computing here
    while (!Thread.currentThread.isInterrupted()){
      System.Out.Println(i);
      i++;
    }


//.... in other part of your program, your main thread for instance:    
    public void stopComptuterThread() {
      computingThread.interrupt();   // assuming computingThread reference reachable
    }

Indeed, some people would use Thread.stop() method.. => here's why it would be very bad: https://www.securecoding.cert.org/confluence/display/java/THI05-J.+Do+not+use+Thread.stop()+to+terminate+threads

Mik378
  • 21,881
  • 15
  • 82
  • 180
0

Thread.stop is deprecated and should not be used. Sample code is here: pause-and-resume-thread

Mak
  • 596
  • 5
  • 10
0

Your code will do. You can use build-in interrupt method, which does mostly the same, but also awakes thread with InterruptedException, if it sleeps/waits. It's good to know, that Java doesn't allow to stop threads "the hard way" (except for using .stop() method on thread, which is deprecated).

So process, in general, looks as following:

  • user requests thread to stop, either by setting a flag (your case) or by invoking .interrupt() method, which sets the flag .interrupted() and "shakes" the thread so it awakes if was sleeping/waiting.

  • it's thread resonsibility to stop it's execution. If you don't implement some logic handling interruption flag, thread could not react to external thread trying to interrupt it and will die after JVM ends it's execution.

Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129
0

Are you sure, that it's thread issue? Have you checked, if .actionPerformed actually calls .stopRunning method?

Anyway, try following code sample. It works for 100%.

class HelloRunner implements Runnable {
    private volatile boolean timeToQuit = false;
    int i = 0;

    public void run() {
        while (!timeToQuit) {
            System.out.println(i);
            i++;
        }
    }

    public void stopRunning() {
        timeToQuit = true;
    }
}

public class MainRunner {
    public static void main(String[] args) throws InterruptedException {
        HelloRunner hr = new HelloRunner();
        Thread tr1 = new Thread(hr, "ThreadOne");

        tr1.start();
        Thread.sleep(100);
        hr.stopRunning();
    }
}
Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129