简体   繁体   English

从可运行类内部中断线程? (java)

[英]Interrupting a thread from inside a runnable class? (java)

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. 编辑:当我试图使取消工作时,我注释掉guiThread.interrupt(),以便我不只是将状态设置为重置状态。

You want to simply call interrupt() - this will interrupt the guiThread, and not the calling thread. 您只想简单地调用interrupt()-这将中断guiThread,而不是调用线程。 Eg 例如

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. 如果线程在中断时将退出,则可以在interrupt()之后添加join(),但这通常不如仅由线程本身进行清理更可取。 (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(). 最后,请注意您的线程不会自动中断并停止正在执行的操作-您需要调用检查中断状态的方法,例如Object.wait(),Thread.sleep()等,或者可以显式检查通过Thread.isInterrupted()获得中断状态。

EDIT: It thought cancel() was on the guiThread. 编辑:它认为cancel()在guiThread上。 It's not, so I've changed the interrupt call. 不是,所以我更改了中断调用。

If you want to do everything inside of cancel , just add a Thread parameter to it and pass a guiThread to it. 如果要在cancel内执行所有操作,只需向其添加Thread参数,然后将guiThread传递给它。

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 );

guiThread.interrupt(); guiThread.interrupt(); should work fine, but if you want to interrupt your thread from inner class method, you should do: 应该可以正常工作,但是如果您想从内部类方法中中断线程,则应该执行以下操作:

public void cancel() {
    if (isAlive()) {
        this.interrupt();
    }
}

or 要么

public void cancel() {
    if (!isInterrupted()) {
        interrupt();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM