这一章节我们来讨论一下暴力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方法的更多相关文章

  1. 从头认识多线程-1.9 迫使线程停止的方法-return法

    这一章节我们来讨论一下还有一种停止线程的方法-return 1.在主线程上面return,是把全部在执行的线程都停掉 package com.ray.deepintothread.ch01.topic ...

  2. Java如何检查一个线程停止或没有?

    Java如何检查一个线程停止或没有? 解决方法 下面的示例演示如何使用 isAlive()方法检查一个线程是否停止. public class Main { public static void ma ...

  3. c# 多线程系列二 自定义线程执行器

    看了第一篇文章,多线程系列,看到了在线程执行任务队列有了一定的了解~! 那么今天我来讲讲,怎么样构建通用的自定义线程概念! 线程执行任务,肯定要有目标,但是如果写死了,那么一个线程处理执行职能按照思路 ...

  4. java多线程总结五:线程池的原理及实现

    1.线程池简介:     多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力.        假设一个服务器完成一项任务所需时间为:T1 创 ...

  5. C#多线程实践——锁和线程安全

    锁实现互斥的访问,用于确保在同一时刻只有一个线程可以进入特殊的代码片段,考虑下面的类: class ThreadUnsafe { static int val1, val2; static void ...

  6. JAVA学习课第二十八届(多线程(七))- 停止-threaded多-threaded面试题

    主密钥 /*  * wait 和 sleep 差别?  * 1.wait能够指定时间也能够不指定  * sleep必须指定时间  * 2.在同步中,对CPU的运行权和锁的处理不同  * wait释放运 ...

  7. 多线程&定时器Timer&同步&线程通信&ThreadLocal

    1.多线程 线程状态分为:新建状态.就绪状态.运行状态.阻塞状态.死亡状态 对象等待池的阻塞状态:运行状态执行了wait方法 对向锁池的阻塞状态:试图获得某个同步锁,已经被其他线程占用,就会放到对象的 ...

  8. Perl多线程(1):解释器线程的特性

    线程简介 线程(thread)是轻量级进程,和进程一样,都能独立.并行运行,也由父线程创建,并由父线程所拥有,线程也有线程ID作为线程的唯一标识符,也需要等待线程执行完毕后收集它们的退出状态(比如使用 ...

  9. Java 多线程(三)—— 线程的生命周期及方法

    这篇博客介绍线程的生命周期. 线程是一个动态执行的过程,它也有从创建到死亡的过程. 线程的几种状态 在 Thread 类中,有一个枚举内部类: 上面的信息以图片表示如下: 第一张图: 第二张图:把等待 ...

随机推荐

  1. centos7 在docker swarm中运行Jenkins,利用gitlab的webhook触发自动部署脚本

    1.宿主机中创建目录 mkdir -p /jenkins_home 2.编辑compose文件,文件名jenkins.yml version: '3.4' services: jenkins-upgr ...

  2. Asp.Net通过ODBC连接Oracle数据库

    本来有个项目是通过安装Oracle client然后让asp.net引用System.Data.OracleClient来访问Oracle数据库的,但是不知道为什么老是报:ORA-12170:连接超时 ...

  3. AllInOneConveter——编码转换工具

    一.Url编码解码 二.Base64编码解码 三.\u形式Unicode和汉互转 四.Md5加密 五.源代码 https://github.com/FrankFan/AllInOneConverter ...

  4. jsp+easyui+DataGrid 例子

    转自:https://blog.csdn.net/l3922768721/article/details/51597977 导入js和css <%@ page language="ja ...

  5. /struts-tags not found ,/struts-dojo-tags not found 上线后异常解决方案

    上线到2003上后发现2个问题:1 缺少/struts-tags2 缺少/struts-dojo-tags在xp上不用直接指定这些文件的位置,但在其他的系统可能无法自动找到它的路径,一定要明确指定在w ...

  6. Git 分支(分布式版本控制系统)

    前言 几乎所有的版本控制系统都以某种形式支持分支.使用分支意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线.在很多版本控制系统中,这是一个略微低效的过程--常常需要完全创建一个源代码目录的 ...

  7. 【struts2】Struts2的系统架构

    Struts2的官方文档里附带了Struts2的架构图,下面这张图上展示了Struts2的内部模块,以及它们的运行流程. 这张图上分了好多块,彼此之间相互联系,先浏览一下各块的名字,再留心一下运行图最 ...

  8. 页面生命周期里面还有很东西,如PageHandlerFactory等等这些东东也够吃一壶的,发现每走到一个领域,发现要学的东西实在是太多太多啦,总感觉自己所学的东西只是沧海一粟,走过了这道坎,又是一片海洋,我只能呐喊:生命永不止息,学海无涯----够用就好。

    页面生命周期里面还有很东西,如PageHandlerFactory等等这些东东也够吃一壶的,发现每走到一个领域,发现要学的东西实在是太多太多啦,总感觉自己所学的东西只是沧海一粟,走过了这道坎,又是一片 ...

  9. eclipse导入class文件

    右键src文件夹->build path->config build path->library->add class folder->create new folder ...

  10. Oracle 12C -- Identity Columns(标识列)

    Identity Columns很适合数据库中需要"surrogate keys"的场景.依赖sequence产生器,每行的标识列会被赋予一个自增或自减的值.缺省,标识列在创建的时 ...