为什么不能使用Thread.stop()方法?
从SUN的官方文档可以得知,调用Thread.stop()方法是不安全的,这是因为当调用Thread.stop()方法时,会发生下面两件事:
1. 即刻抛出ThreadDeath异常,在线程的run()方法内,任何一点都有可能抛出ThreadDeath Error,包括在catch或finally语句中。
2. 释放该线程所持有的所有的锁
当线程抛出ThreadDeath异常时,会导致该线程的run()方法突然返回来达到停止该线程的目的。ThreadDetath异常可以在该线程run()方法的任意一个执行点抛出。但是,线程的stop()方法一经调用线程的run()方法就会即刻返回吗?
Java代码
;i<100000;i++) //Givettimetogetgoing…t.stop();//EXPECTCOMPILERWARNING
public static void main(String[] args) {try {Thread t = new Thread() {public synchronized void run() {try {long start=System.currentTimeMillis();for (int i = 0; i < 100000; i++)System.out.println("runing.." + i);System.out.println((System.currentTimeMillis()-start)/1000);} catch (Throwable ex) {System.out.println("Caught in run: " + ex);ex.printStackTrace();}}};t.start();// Give t time to get going…Thread.sleep(100);t.stop(); // EXPECT COMPILER WARNING} catch (Throwable t) {System.out.println("Caught in main: " + t);t.printStackTrace();}}
假设我们有如上一个工作线程,它的工作是数数,从1到1000000,我们的目标是在它进行数数的过程中,停止该线程的运作。如果我们按照上面的方式来调用thread.stop()方法,原则上是可以实现我们的目标的,根据SUN官方文档的解释,加上在上面的程序中,主线程只休眠了100ms,而工作线程从1数到1000000所花时间大概是4-5s,那么该工作线程应该只从1数到某个值(小于1000000),然后线程停止。
但是根据运行结果来看,并非如此。
结果:
。。。
runing..99998
runing..99999
5
。。。
runing..99998
runing..99999
4
每次运行的结果都表明,工作线程并没有停止,而是每次都成功的数完数,然后正常中止,而不是由stop()方法进行终止的。这个是为什么呢?根据SUN的文档,原则上只要一调用thread.stop()方法,那么线程就会立即停止,并抛出ThreadDeath error,查看了Thread的源代码后才发现,原先Thread.stop0()方法是同步的,而我们工作线程的run()方法也是同步,那么这样会导致主线程和工作线程共同争用同一个锁(工作线程对象本身),由于工作线程在启动后就先获得了锁,所以无论如何,当主线程在调用t.stop()时,它必须要等到工作线程的run()方法执行结束后才能进行,结果导致了上述奇怪的现象。
把上述工作线程的run()方法的同步去掉,再进行执行,结果就如上述第一点描述的那样了
可能的结果:
runing..4149runing..4150runing..4151runing..4152runing..4152Caught in run: java.lang.ThreadDeath
或者
runing..5245runing..5246runing..5247runing..5248runing..5248Caught in run: java.lang.ThreadDeath
接下来是看看当调用thread.stop()时,被停止的线程会不会释放其所持有的锁,看如下代码:
Java代码
//sleepfor3s//Givettimetogetgoing…
public static void main(String[] args) {final Object lock = new Object();try {Thread t0 = new Thread() {public void run() {try {synchronized (lock) {System.out.println("thread->" + getName()+ " acquire lock.");sleep(3000);// sleep for 3sSystem.out.println("thread->" + getName()+ " release lock.");}} catch (Throwable ex) {System.out.println("Caught in run: " + ex);ex.printStackTrace();}}};Thread t1 = new Thread() {public void run() {synchronized (lock) {System.out.println("thread->" + getName()+ " acquire lock.");}}};t0.start();// Give t time to get going…Thread.sleep(100);//t0.stop();t1.start();} catch (Throwable t) {System.out.println("Caught in main: " + t);t.printStackTrace();}}
当没有进行t0.stop()方法的调用时, 可以发现,,两个线程争用锁的顺序是固定的。
输出:
thread->Thread-0 acquire lock.thread->Thread-0 release lock.thread->Thread-1 acquire lock.
但调用了t0.stop()方法后,(去掉上面的注释//t0.stop();),可以发现,t0线程抛出了ThreadDeath error并且t0线程释放了它所占有的锁。
输出:
积极思考造成积极人生,消极思考造成消极人生。