java - Java中如何终止这样的线程

标签 java multithreading

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/

相关文章:

java - ElasticSearch 日期映射

java - 为什么我的 ImageView 不获取位图并显示它?

Java Thread.sleep(延迟)不起作用

java - Redis中不同的连接异常

java - 更改 JSpinner 中的日期

java - 在屏幕上展开 DataGrid 将使其所有项目可见

c - 线程屏障同步

python - 在 Python 中处理异步

java - 在 Java 中,如何确保 boolean 标志的安全和一致的并发使用,同时最大限度地减少对时间性能的影响?

java - 字符串列表(多行)作为 Java 中的命令行输入