中断线程详解(Interrupt)
在上篇文章《多线程的使用——Thread类和Runnable接口》中提到中断线程的问题。在JAVA中,曾经使用stop方法来停止线程,然而,该方法具有固有的不安全性,因而已经被抛弃(Deprecated)。那么应该怎么结束一个进程呢?官方文档中对此有详细说明:《为何不赞成使用 Thread.stop、Thread.suspend 和 Thread.resume?》。在此引用stop方法的说明:
- private Thread blinker;
- public void start() {
- blinker = new Thread(this);
- blinker.start();
- }
- public void stop() {
- blinker.stop(); // UNSAFE!
- }
- public void run() {
- Thread thisThread = Thread.currentThread();
- while (true) {
- try {
- thisThread.sleep(interval);
- } catch (InterruptedException e){
- }
- repaint();
- }
- }
- private volatile Thread blinker;
- public void stop() {
- blinker = null;
- }
- public void run() {
- Thread thisThread = Thread.currentThread();
- while (blinker == thisThread) {
- try {
- thisThread.sleep(interval);
- } catch (InterruptedException e){
- }
- repaint();
- }
- }
- package com.polaris.thread;
- public class TestThread implements Runnable{
- boolean stop = false;
- public static void main(String[] args) throws Exception {
- Thread thread = new Thread(new TestThread(),"My Thread");
- System.out.println( "Starting thread..." );
- thread.start();
- Thread.sleep( 3000 );
- System.out.println( "Interrupting thread..." );
- thread.interrupt();
- System.out.println("线程是否中断:" + thread.isInterrupted());
- Thread.sleep( 3000 );
- System.out.println("Stopping application..." );
- }
- public void run() {
- while(!stop){
- System.out.println( "My Thread is running..." );
- // 让该循环持续一段时间,使上面的话打印次数少点
- long time = System.currentTimeMillis();
- while((System.currentTimeMillis()-time < 1000)) {
- }
- }
- System.out.println("My Thread exiting under request..." );
- }
- }
- package com.polaris.thread;
- public class TestThread2 implements Runnable{
- boolean stop = false;
- public static void main(String[] args) throws Exception {
- Thread thread = new Thread(new TestThread2(),"My Thread2");
- System.out.println( "Starting thread..." );
- thread.start();
- Thread.sleep( 3000 );
- System.out.println( "Interrupting thread..." );
- thread.interrupt();
- System.out.println("线程是否中断:" + thread.isInterrupted());
- Thread.sleep( 3000 );
- System.out.println("Stopping application..." );
- }
- public void run() {
- while(!stop){
- System.out.println( "My Thread is running..." );
- // 让该循环持续一段时间,使上面的话打印次数少点
- long time = System.currentTimeMillis();
- while((System.currentTimeMillis()-time < 1000)) {
- }
- if(Thread.currentThread().isInterrupted()) {
- return;
- }
- }
- System.out.println("My Thread exiting under request..." );
- }
- }

注意:interrupted与isInterrupted方法的区别(见API文档)
引用一篇文章
来自随心所欲http://redisliu.blog.sohu.com/131647795.html的《Java的interrupt机制》
当外部线程对某线程调用了thread.interrupt()方法后,java语言的处理机制如下:
如果该线程处在可中断状态下,(调用了xx.wait(),或者Selector.select(),Thread.sleep()等特定会发生阻塞的api),那么该线程会立即被唤醒,同时会受到一个InterruptedException,同时,如果是阻塞在io上,对应的资源会被关闭。如果该线程接下来不执行“Thread.interrupted()方法(不是interrupt),那么该线程处理任何io资源的时候,都会导致这些资源关闭。当然,解决的办法就是调用一下interrupted(),不过这里需要程序员自行根据代码的逻辑来设定,根据自己的需求确认是否可以直接忽略该中断,还是应该马上退出。
如果该线程处在不可中断状态下,就是没有调用上述api,那么java只是设置一下该线程的interrupt状态,其他事情都不会发生,如果该线程之后会调用行数阻塞API,那到时候线程会马会上跳出,并抛出InterruptedException,接下来的事情就跟第一种状况一致了。如果不会调用阻塞API,那么这个线程就会一直执行下去。除非你就是要实现这样的线程,一般高性能的代码中肯定会有wait(),yield()之类出让cpu的函数,不会发生后者的情况。
中断线程详解(Interrupt)的更多相关文章
- linux-2.6.26内核中ARM中断实现详解(转)
转载:http://www.cnblogs.com/leaven/archive/2010/08/06/1794293.html 更多文档参见:http://pan.baidu.com/s/1dDvJ ...
- POSIX 线程详解(经典必看)
http://www.cnblogs.com/sunminmin/p/4479952.html 总共三部分: 第一部分:POSIX 线程详解 ...
- 通用线程:POSIX 线程详解,第 3 部分 条件互斥量(pthread_cond_t)
使用条件变量提高效率 本文是 POSIX 线程三部曲系列的最后一部分,Daniel 将详细讨论如何使用条件变量.条件变量是 POSIX 线程结构,可以让您在遇到某些条件时“唤醒”线程.可以将它们看作是 ...
- “全栈2019”Java多线程第二十五章:生产者与消费者线程详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- python线程详解
#线程状态 #线程同步(锁)#多线程的优势在于可以同时运行多个任务,至少感觉起来是这样,但是当线程需要共享数据时,可能存在数据不同步的问题. #threading模块#常用方法:'''threadin ...
- 通用线程:POSIX 线程详解,第 3 部分
通用线程:POSIX 线程详解,第 3 部分 使用条件变量提高效率 Daniel Robbins, 总裁兼 CEO, Gentoo Technologies, Inc. 简介: 本文是 POSIX 线 ...
- mysql后台线程详解
1.mysql后台线程 mysql后台线程主要用于维持服务器的正常运行和完成用户提交的任务,主要包括:master thread,read thread,write thread,redo log t ...
- Python进阶----线程基础,开启线程的方式(类和函数),线程VS进程,线程的方法,守护线程,详解互斥锁,递归锁,信号量
Python进阶----线程基础,开启线程的方式(类和函数),线程VS进程,线程的方法,守护线程,详解互斥锁,递归锁,信号量 一丶线程的理论知识 什么是线程: 1.线程是一堆指令,是操作系统调度 ...
- KafkaProducer Sender 线程详解(含详细的执行流程图)
目录 1.Sender 线程详解 2.RecordAccumulator 核心方法详解 温馨提示:本文基于 Kafka 2.2.1 版本. 上文 <源码分析 Kafka 消息发送流程> 已 ...
随机推荐
- HDUOJ--点球大战
点球大战 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- DBA_实践指南系列1_Oracle Erp R12系统安装Install(案例)
2013-12-01 Created By BaoXinjian
- Shell习题100例
每日一文件 https://github.com/aminglinux/shell100/blob/master/ 要求:安照这样的日期格式(xxxx-xx-xx)每日生成一个文件,如生成的文件为20 ...
- Python 元组 min() 方法
描述 Python 元组 min() 方法返回元组中元素最小值. 语法 min() 方法语法: min(T) 参数 T -- 指定的元组. 返回值 返回元组中元素最小值. 实例 以下实例展示了 min ...
- 【iOS开发-56】案例BUG:button的enabled、控件的userInteractionEnabled以及两种提示框UIAlert和UIActionSheet
接上述案例找BUG:[iOS开发-51]案例学习:动画新写法.删除子视图.视图顺序.延迟方法.button多功能使用方法及icon图标和启动页设置 (1)BUG:答案满了就不能再点击optionbut ...
- win7 64 安装scikit-learn
1. scikit-learn简单介绍 scikit-learn是一个基于NumPy.SciPy.Matplotlib的开源机器学习工具包.採用Python语言编写.主要涵盖分类. 回归和聚类等算法, ...
- mysql - 语法复习与学习
//本月的第一天,最后一天 $start=date('Y-m-01', strtotime(date("Y-m-d"))); echo date('Y-m-d', strtotim ...
- 3dmax坐标系与导出fbx的坐标系
3dmax和opengl都是右手坐标系,但是3dmax是z轴向上,而opengl中是Y轴向上.如图: 所以在3dmax的fbx导出对话框中有“轴转化”一项,可以设置“Y向上”或者“Z向上”. 默认是“ ...
- Opening Default document on IIS (HTML With WebAPI)
Question: I've a deployed ASP.NET Web API with a website on the same folder that consume it. When I ...
- Socket网络编程 详细过程(转)
我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览器浏览网页时,浏览器的进程怎么与web服务器通信的?当你用QQ聊天时,QQ进程怎么与服务器或你好友所在的QQ进程通信?这些都得靠so ...