Possible Duplicate:
How to kill a java thread ?
我的应用程序中有如下线程。
我怎样才能终止它们?
new Thread(new Runnable() {
public void run() {
// do it…
}
}).start();
编辑:
解决方案:
Class Memory{
static Runnable last;
}
This will save our Thread in a var.
new Thread(new Runnable() {
public void run() {
Memory.last = this;
// do it…
}
}).start();
now in any part you want stop:
Memory.last.wait(); //will pause but next new thread will terminate it (garbage collector do this );
最佳答案
在 run() 方法中,您可以定期调用 interrupted()
来测试线程是否被中断,然后静默退出或抛出 InterruptedException。要停止线程,您可以调用它的 interrupt()
方法。请参阅Java tutorial in interrupts了解更多信息。
关于java - Java中如何终止这样的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5971083/