从头认识多线程-1.8 迫使线程停止的方法-暴力Stop方法

Posted yangykaifa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从头认识多线程-1.8 迫使线程停止的方法-暴力Stop方法相关的知识,希望对你有一定的参考价值。

这一章节我们来讨论一下暴力Stop方法。

1.使用样例

package com.ray.deepintothread.ch01.topic_8;

public class StopByStopMethod {
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws InterruptedException {
		ThreadFive threadFive = new ThreadFive();
		threadFive.start();
		Thread.sleep(200);
		threadFive.stop();
	}
}

class ThreadFive extends Thread {

	@Override
	public void run() {
		System.out.println("------------begin-------------");
		try {
			System.out.println("------------working-------------");
			sleep(2000);
		} catch (InterruptedException e) {
			System.out.println("------------exit-------------");
		}
		super.run();
	}
}

输出:

------------begin-------------
------------working-------------


2.这是一个java弃用的方法,使用它的时候存在重大隐患

以下是引用java的api里面的原文

* Forces the thread to stop executing.
     * <p>
     * If there is a security manager installed, its <code>checkAccess</code>
     * method is called with <code>this</code>
     * as its argument. This may result in a
     * <code>SecurityException</code> being raised (in the current thread).
     * <p>
     * If this thread is different from the current thread (that is, the current
     * thread is trying to stop a thread other than itself), the
     * security manager‘s <code>checkPermission</code> method (with a
     * <code>RuntimePermission("stopThread")</code> argument) is called in
     * addition.
     * Again, this may result in throwing a
     * <code>SecurityException</code> (in the current thread).
     * <p>
     * The thread represented by this thread is forced to stop whatever
     * it is doing abnormally and to throw a newly created
     * <code>ThreadDeath</code> object as an exception.
     * <p>
     * It is permitted to stop a thread that has not yet been started.
     * If the thread is eventually started, it immediately terminates.
     * <p>
     * An application should not normally try to catch
     * <code>ThreadDeath</code> unless it must do some extraordinary
     * cleanup operation (note that the throwing of
     * <code>ThreadDeath</code> causes <code>finally</code> clauses of
     * <code>try</code> statements to be executed before the thread
     * officially dies).  If a <code>catch</code> clause catches a
     * <code>ThreadDeath</code> object, it is important to rethrow the
     * object so that the thread actually dies.
     * <p>
     * The top-level error handler that reacts to otherwise uncaught
     * exceptions does not print out a message or otherwise notify the
     * application if the uncaught exception is an instance of
     * <code>ThreadDeath</code>.

大致的意思:

(1)当程序调用security manager的时候,会额外的运行checkPermission方法

(2)当程序调用security manager,会抛出SecurityException这样的安全异常

(3)隐式抛ThreadDeath异常

(4)同意stop那些还没有開始的线程

(5)即便那些线程启动了。也会立马终止

等等,还有几个

因此。这种方法终于被java弃用。


我们来演示一下第三个原因,由于其它的几个比較难通过一个样例就行说清楚。

package com.ray.deepintothread.ch01.topic_8;

public class CatchThreadDeath {
	public static void main(String[] args) throws InterruptedException {
		ThreadOne threadOne = new ThreadOne();
		threadOne.start();
		Thread.sleep(200);
	}
}

class ThreadOne extends Thread {

	@SuppressWarnings("deprecation")
	@Override
	public void run() {
		try {
			this.stop();
		} catch (ThreadDeath e) {
			System.out.println(e);
		}
		super.run();
	}
}

输出:

java.lang.ThreadDeath


总结:这一章节我们来讨论了一下暴力stop线程,然后描写叙述了一下他的原因。


我的github:https://github.com/raylee2015/DeepIntoThread



以上是关于从头认识多线程-1.8 迫使线程停止的方法-暴力Stop方法的主要内容,如果未能解决你的问题,请参考以下文章

从头认识多线程-2.24 修改监视器对同步的影响

从头认识多线程-4.3 ThreadLocal使用时需要注意的地方

JAVA-初步认识-第十四章-多线程-停止线程方式-定义标记

JAVA-初步认识-第十四章-多线程-停止线程方式-interrupt

从头认识多线程-1.16 对照不同的优先级

从头认识多线程-3.1 使用volatile解决异步死循环