1、stop
对于stop方法会立即停止某个运行线程的执行,不管正在执行的线程在执行什么正常操作。所以如果直接stop 可能会导致文件不会正常关闭,try catch 也不会再执行。 另外 stop 会立即释放运行线程它锁定的监视器对象。这样会导致其他线程提前得到了未完全“初始化”的数据,导致数据不一致。
This method is inherently unsafe. Stopping a thread with
* Thread.stop causes it to unlock all of the monitors that it
* has locked (as a natural consequence of the unchecked
*ThreadDeath
exception propagating up the stack). If
* any of the objects previously protected by these monitors were in
* an inconsistent state, the damaged objects become visible to
* other threads, potentially resulting in arbitrary behavior. Many
* uses ofstop
should be replaced by code that simply
* modifies some variable to indicate that the target thread should
* stop running. The target thread should check this variable
* regularly, and return from its run method in an orderly fashion
* if the variable indicates that it is to stop running. If the
* target thread waits for long periods (on a condition variable,
* for example), theinterrupt
method should be used to
* interrupt the wait.
2、Thread suspend
This method has been deprecated, as it is
* inherently deadlock-prone. If the target thread holds a lock on the
* monitor protecting a critical system resource when it is suspended, no
* thread can access this resource until the target thread is resumed. If
* the thread that would resume the target thread attempts to lock this
* monitor prior to callingresume
, deadlock results. Such
* deadlocks typically manifest themselves as “frozen” processes.
上面的英文很好的解释了为什么不建议使用suspend 理由是 suspend 不会释放 线程所持有的的锁,那么,只能由resume 来唤醒其继续执行,那么当resume线程想要获取suspend线程所持有的的锁,然后在调用resume 那么就会发生死锁。
3、resume 存在的意义就是将suspend唤醒 当suspend不建议使用的时候 其也没有存在的意义。