In this chapter we will discuss the violence stop method.
1. Use the sample
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 ($); Threadfive.stop ();}} Class Threadfive extends Thread {@Overridepublic void run () {System.out.println ("------------begin-------------"); try {System.out.println ("------------working-------------"); Sleep (2000);} catch (Interruptedexception e) {System.out.println ("------------exit-------------");} Super.run ();}}
Output:
------------begin-------------
------------Working-------------
2. This is a Java deprecated method, the use of which there is a major hidden danger
The following is the original text in the API referencing Java
* Forces the thread to stop executing. * <p> * If There is a security manager installed, it <code>checkAccess</code> * method is Calle D with <code>this</code> * as its argument. This could result in a * <code>SecurityException</code> being raised (in the current thread). * <p> * If This thread was different from the current thread (which is, the current * thread was trying to stop A thread other than itself), the * Security Manager's <code>checkPermission</code> method (with a * & Lt;code>runtimepermission ("Stopthread") </code> argument) are called in * addition. * Again, this could result in throwing a * <code>SecurityException</code> (in the current thread). * <p> * The thread represented by this thread was forced to stop whatever * it's doing abnormally and to THR OW a newly created * <code>ThreadDeath</code> object as an exceptIon. * <p> * It is permitted-stop a thread that have 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 mus t do some extraordinary * cleanup operation (Note that the throwing of * <code>ThreadDeath</code> Caus ES <code>finally</code> Clauses of * <code>try</code> statements to be executed before the THR EAD * officially dies). If a <code>catch</code> clause catches a * <code>ThreadDeath</code> object, it's important to Rethrow the * object so, the thread actually dies. * <p> * The top-level error handler that reacts to otherwise uncaught * Exceptions does not print out a mess Age or otherwise notify the * application if the uncaught exception was an instance of * <code>threaddeath< ;/code>.
The General meaning:
(1) When the program calls Security Manager, additional checkpermission methods are run
(2) When the program calls security manager, it throws SecurityException
(3) Implicit parabolic threaddeath anomalies
(4) Agree to stop those threads that have not yet started
(5) Even if those threads start up. It will terminate immediately.
Wait, there's a couple more.
So. This method was finally deprecated by Java.
Let's demonstrate the third reason, because the other few are more difficult to make clear by a sample.
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") @Overridepublic void Run () {try {this.stop ();} catch ( Threaddeath e) {System.out.println (e);} Super.run ();}}
Output:
Java.lang.ThreadDeath
Summary: In this chapter we discuss the violent stop thread, and then describe the reason for his explanation.
My github:https://github.com/raylee2015/deepintothread.
Understanding multithreading-1.8 ways to force threads to stop-violence stop method