Question

I am trying to interrupt a normally running thread (which is not in sleep() or wait() state) .

while going through in net i got to know interrupting a normally running thread will just set the flag true and continue the process.

Code snippet is

one.java

......
......
actionperformedmethod {

if (actionCmd.equals("cancel")) {
    try {
        r1.stop();  // to two.java
    } catch (InterruptedException ex) {
        ....
        ....
    }
}
}

in two.java

.....
.....
stop method() throws InterruptedException{
        if(!(t.isInterrupted())){
            t.interrupt();
            throw new InterruptedException();
        }
}

from two.java when i throw InterruptedException i can able to get the exception block at one.java , but how do i stop the thread after that because even after that thread seems to continue the normal process.

Am new to thread concepts please help..

Was it helpful?

Solution 2

Interrupt will not stop the thread. it just sets the flag to true to signal the thread to stop the execution soon.

to stop the execution add global variable as

private volatile boolean exit = false;

and you add one method in your 2nd class

public void requestExit(){
  exit = true;
 }

inside run () of your thread do something like this

 if (exit == true){
    return;
   }

whenever you want to call just call this method requestExit() from your main() or wherever you want to stop

this is the best way to stop the thread.. using stop() on thread is dangerous as it does not clear any resources and its not advisable to use even by oracle hence deprecated.

let me know for any issues

OTHER TIPS

The interrupt() method is co-operative rather than pre-emptive - the background task needs to actively check Thread.interrupted() at suitable intervals, and take action to shut itself down cleanly.

public void run() {
  openSomeResources();
  try {
    while(notFinished) {
      if(Thread.interrupted()) return;
      doSomeStuff();
    }
  } finally {
    closeTheResources();
  }
}

In this example if the thread is interrupted in the middle of doSomeStuff() then it will complete the current "iteration" before responding to the interruption. Getting the correct balance between responding promptly to an interrupt on the one hand, and responding only at a safe point in the execution on the other hand, is something that is inherently specific to the particular task - there is no one-size-fits-all answer.

Note however that any blocking method that throws an InterruptedException will reset the interrupt flag when this exception is thrown. Therefore in order for this sort of checking to work you must re-interrupt yourself whenever you receive an InterruptedException

try {
  Thread.sleep(3000);
} catch(InterruptedException e) {
  // we were interrupted - set the flag so the next interrupted() check will
  // work correctly.
  Thread.currentThread().interrupt();
}

Threads are only running whilst their run() method is on the stack so usually people put a while(true) inside the run method, all you need to do in you thread to stop it is to return somewhere in the run method or break the loop then as soon as the run() method is no longer running the thread has been stopped.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top