1.7.6方法stop()与java.lang.threadDeath异常

调用stop方法时会抛出java.lang.ThreadDeath异常,但一般情况下这个异常不需要显示的捕捉

 1 package com.cky.thread;
 2 
 3 /**
 4  * Created by edison on 2017/12/3.
 5  */
 6 public class MyThread extends Thread{
 7     @Override
 8     public void run() {
 9         super.run();
10         try {
11            this.stop();
12         } catch (ThreadDeath e) {
13             e.printStackTrace();
14         }
15     }
16 }
 1 package com.cky.test;
 2 
 3 import com.cky.thread.MyThread;
 4 
 5 /**
 6  * Created by edison on 2017/12/3.
 7  */
 8 public class Test {
 9     public static void main(String[] args) {
10             MyThread th = new MyThread();
11             th.start();
12     }
13 }
C:itsoftjdkinjava -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext
ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib
esources.jar;C:itsoftjdkjrelib
t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
java.lang.ThreadDeath
    at java.lang.Thread.stop(Thread.java:853)
    at com.cky.thread.MyThread.run(MyThread.java:12)

Process finished with exit code 0

方法stop已经作废

因为如果强制让线程停止则会使得一些清理性的工作得不到完成,另外一个就是对锁定对象进行解锁,导致数据得不到同步处理,出现数据不一致。

原文地址:https://www.cnblogs.com/edison20161121/p/7954761.html