本文介绍了如何杀死java线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我谷歌杀死java线程的解决方案。并且存在两种解决方案:

I google the solution for killing a java thread. And there exists two solutions:


  1. 使用Thread.interrupt设置标志


但他们两个都不适合我。在我的帖子中,我称第三方api需要很长时间才能完成。如果花费太多时间,我想让用户取消这个帖子。

But both of them are not suitable for me. In my thread, I call a third-party api which takes a long time to complete. and I want to let the user to cancel this thread if it takes too much time.

那我怎么能杀掉这个帖子呢?在此先感谢。

So how can I kill this thread ? Thanks in advance.

推荐答案

Thread.interrupt()是唯一的通常适用的安全方法。您当然可以使用其他应用程序级信号(例如对volatile变量进行条件检查)来自行终止。其他方法(例如所有已弃用的Thread.xx方法)可能会以非确定性方式污染应用程序的状态,并且需要重新加载所有应用程序状态。

Thread.interrupt() is the only safe method which is generally applicable. You can of course use other application-level signals (such as a conditional check on a volatile variable) to self-terminate. Other methods (such as all the deprecated Thread.xx methods) can pollute your application's state in non-deterministic ways, and would require reloading all application state.

这篇关于如何杀死java线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:58