Solace
Solace

Reputation: 9020

Threads: How to interrupt a thread from outside of that thread

I have a simple GUI in which there are two buttons: Print and Stop.

When the user presses print, an already saved number is printed continuously in a loop.

When the user presses Stop, the printing stops.

I am handling the printing of the number in a separate thread, because I need the thread to sleep for a millisecond before printing again.

printBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                Thread a= new Thread(new Runnable(){
                    public void run(){
                        textArea.setText("");
                        for (int i=0; i<10; i++){
                            int result= 0;
                            System.out.println(result+"\n"); 
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            } 
                        }
                    }
                });
                a.start();
            }
        });

Now in the ActionListener of the Stop button, I want the first thread to be interrupted or stopped. How can I do that, since it needs to be interrupted from another thread?

Upvotes: 0

Views: 1855

Answers (3)

stan
stan

Reputation: 1014

final Thread a= new Thread(new Runnable(){
    public void run(){
        try {
            textArea.setText("");
            for (int i=0; i<10; i++){
                int result= 0;
                System.out.println(result+"\n"); 
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            // ignore
        } 
    }
});

printBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

        a.start();
    }
});

stopBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

       if( a.isAlive() ) {   
           a.interrupt();
       }
    }
});

Upvotes: 1

Smutje
Smutje

Reputation: 18173

If your first thread contains no Thread-blocking operation, you could for example check for a flag in the for-loop which gets set to true when you press the "Stop"-button.

public class WriterThread implements Runnable {

    private volatile boolean stopped = false;

    public synchronized void stop() {
        this.stopped = true;
    }

    public void run(){
            textArea.setText("");

            for (int i=0; i<10; i++) {
                    if (this.stopped) {
                            break;
                    }

                    int result= 0;
                    System.out.println(result+"\n");

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } 
                }
            }
        }
    }
}

Upvotes: 3

Roman K
Roman K

Reputation: 3337

Use AtomicBoolean as flag. It will ensure the thread safety.

Runnable r= new Runnable(){

    private AtomicBoolean stop= new AtomicBoolean(false);

    public void run(){
        for(...){

           if(stop.get()){
               break; // break the loop
           }

           ...

        }
    }

    public stop(){
        stop.set(true);
    }
}

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

Upvotes: 1

Related Questions