结束一个进程用到了Thread.stop() ,可是现在这种方法已经不提倡用了,那怎么结束一个线程呢??
我用Thread.stop() 结束一个线程后,抛出一个ThreadDeath 的对象,其是Error的子类,还用捕获吗??怎么捕获它呢?

解决方案 »

  1.   

    绝对没事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.
      

  2.   

    线程会以以下三种方式之一结束:
    线程到达其 run() 方法的末尾。
    线程抛出一个未捕获到的 Exception 或 Error。
    另一个线程调用一个弃用的 stop() 方法。最好是
    int flag = 1;
    public void run() {        while(flag==1) {
             ....
            }
    }
    要停止时设置flag=0, 线程自动结束循环而结束