views:

234

answers:

2

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.

+2  A: 

You want to simply call interrupt() - this will interrupt the guiThread, and not the calling thread. E.g.

public void cancel()

{

  guiThread.interrupt();

  // other clean up code here.

}

However, are you sure you want the cleanup code running on the calling thread? It is usually best to have the thread itself do its own cleanup. You don't know when the thread is interrupted and ready to be cleaned up. You could add a join() after interrupt() if the thread will exit when interrupted, but this is generally less preferable to simply having the thread itself do the cleanup. (Later, you may not even have separate threads for these tasks, but use a thread pool. Putting cleanup in with the task will make this much easier to manage.)

Finally, please be aware that your thread doesn't automatically interrupt and stop what it's doing - you need to call methods that check the interrupt status, such as Object.wait(), Thread.sleep() etc. or you can explicitly check the interrupt status via Thread.isInterrupted().

EDIT: It thought cancel() was on the guiThread. It's not, so I've changed the interrupt call.

mdma
Additionally, you probably don't want to be running your GUI on a standard thread. you should be sure to run it on the Event Dispatch Thread.
Jeff Storey
You can't call `interrupt`. `cancel` is a method of `Gui` and it's not a `Thread`, but `Runnable`.
Alexander Pogrebnyak
@Alexander - thanks for picking that up. I've edited my answer.
mdma
Wow, why i didn't think just to do the cleanup in the finally block is beyond me, i literally did the exact same thing on 2 other threads, and just didn't think to do it here (too much time in front of the computer). Anyways this is a much better solution, thanks :)
kyeana
No worries, glad to help. I know that feeling - after a while you know the code so well you almost stop seeing it.
mdma
+2  A: 

If you want to do everything inside of cancel, just add a Thread parameter to it and pass a guiThread to it.

void cancel ( final Thread guiThread )
{
  guiThread.interrupt( );

  guiThread.join( );

  // other cleanup code
  ...
}

Caller code

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

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

guiThread.start()

...

...

gui.cancel( guiThread );
Alexander Pogrebnyak