你如何在 Java 中杀死一个线程? [英] How do you kill a Thread in Java?

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

问题描述

你如何在 Java 中杀死一个 java.lang.Thread?

How do you kill a java.lang.Thread in Java?

推荐答案

参见此 Sun 的关于他们为何弃用 Thread.stop() 的帖子.它详细说明了为什么这是一个糟糕的方法,以及通常应该采取什么措施来安全地停止线程.

See this thread by Sun on why they deprecated Thread.stop(). It goes into detail about why this was a bad method and what should be done to safely stop threads in general.

他们推荐的方式是使用共享变量作为要求后台线程停止的标志.然后可以由请求线程终止的不同对象设置此变量.

The way they recommend is to use a shared variable as a flag which asks the background thread to stop. This variable can then be set by a different object requesting the thread terminate.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆