结束一个进程用到了Thread.stop() ,可是现在这种方法已经不提倡用了,那怎么结束一个线程呢??
我用Thread.stop() 结束一个线程后,抛出一个ThreadDeath 的对象,其是Error的子类,还用捕获吗??怎么捕获它呢?
我用Thread.stop() 结束一个线程后,抛出一个ThreadDeath 的对象,其是Error的子类,还用捕获吗??怎么捕获它呢?
解决方案 »
- 反编译会隐藏子类构造函数里面的super()语句吗?
- 介绍下java printf方法
- pack200.exe压缩后的JAR文件如何用呀?似乎压缩后的文件后辍名一定得是.gz,可这个文件能直接像原来的JAR文件一样使用吗?
- 如何让应用程序只能启动一次啊????????
- 多线程同步问题
- 各位大哥大姐 能否说一下jBuilder 里的一些热键,象单步调试的是哪个,谢谢
- 提示已定义类型Employee错误
- TOMCAT的问题!!!!!!!!!!!!!
- adsl怎么共享上网?
- JAXB中如何指定包名?
- [百分帖]web.xml相关,主要是涉及到多个servet的定义!
- 请教怎样在mysql数据库上设密码啊?能不能说说具体的操作步骤啊?谢谢!
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.
线程到达其 run() 方法的末尾。
线程抛出一个未捕获到的 Exception 或 Error。
另一个线程调用一个弃用的 stop() 方法。最好是
int flag = 1;
public void run() { while(flag==1) {
....
}
}
要停止时设置flag=0, 线程自动结束循环而结束