新手提问!!大侠帮忙呀!!

hot_baby 2005-12-13 10:06:19
结束一个进程用到了Thread.stop() ,可是现在这种方法已经不提倡用了,那怎么结束一个线程呢??
我用Thread.stop() 结束一个线程后,抛出一个ThreadDeath 的对象,其是Error的子类,还用捕获吗??怎么捕获它呢?
...全文
77 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lip009 2005-12-13
  • 打赏
  • 举报
回复
同意楼上
cenlmmx 2005-12-13
  • 打赏
  • 举报
回复
线程会以以下三种方式之一结束:
线程到达其 run() 方法的末尾。
线程抛出一个未捕获到的 Exception 或 Error。
另一个线程调用一个弃用的 stop() 方法。

最好是
int flag = 1;
public void run() {

while(flag==1) {
....
}
}
要停止时设置flag=0, 线程自动结束循环而结束
believefym 2005-12-13
  • 打赏
  • 举报
回复
绝对没事sun干吗把它给deprecate?
stop中止线程的时候不释放lock,容易引起死锁

Why is Thread.stop deprecated?
Because it is inherently unsafe. Stopping a thread causes it to unlock all the monitors that it has locked. (The monitors are unlocked as the ThreadDeath exception propagates up the stack.) If any of the objects previously protected by these monitors were in an inconsistent state, other threads may now view these objects in an inconsistent state. Such objects are said to be damaged. When threads operate on damaged objects, arbitrary behavior can result. This behavior may be subtle and difficult to detect, or it may be pronounced. Unlike other unchecked exceptions, ThreadDeath kills threads silently; thus, the user has no warning that his program may be corrupted. The corruption can manifest itself at any time after the actual damage occurs, even hours or days in the future.
blueindead 2005-12-13
  • 打赏
  • 举报
回复
就用stop,绝对没事儿

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧