Understanding multithreading-1.8 ways to force threads to stop-violence stop method

Source: Internet
Author: User
Tags deprecated

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&lt ;/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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.