Interrupting a thread from inside a runnable class? (java)

Posted by kyeana on Stack Overflow See other posts from Stack Overflow or by kyeana
Published on 2010-05-08T22:51:01Z Indexed on 2010/05/08 22:58 UTC
Read the original article Hit count: 109

Filed under:
|
|

I am trying to set up a method inside a class that implements the runnable interface that will set the interrupt status of that class. The reason i want to be able to do it from inside the class is there is some other clean up stuff that i need to take care of as well, and i would like to be able to do it all by calling one method instead of calling, for example:

Gui gui = new Gui() // class that implements runnable

Thread guiThread = new Thread(gui, "gui thread");

guiThread.start()

...

...

guiThread.interrupt();

gui.cancel();

Currently my cancel code looks like this, however it isn't correctly setting the interrupt status of this thread.

public void cancel()

{

  Thread.currentThread().interrupt();

  // other clean up code here.

}

Any advice on if/how i could get this working?

Thanks.

EDIT: I when i tried to get the cancel working, i commented out the guiThread.interrupt(), so that i wasn't just setting the status the reseting the status.

© Stack Overflow or respective owner

Related posts about java

Related posts about threads