Java并发编程(二)线程任务的中断(interrupt)
使用interrupt()中断线程
当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它,该方法只是在目标线程中设置一个标志,表示它已经被中断,并立即返回。
package com.csdhsm.concurrent; import java.util.concurrent.TimeUnit; /**
* @Title: InterruptDemo.java
* @Package: com.csdhsm.concurrent
* @Description 中断Demo
* @author Han
* @date 2016-4-18 下午6:55:00
* @version V1.0
*/ public class InterruptDemo implements Runnable { @Override
public void run() { System.out.println("The thread is runing.");
System.out.println("The thread is sleeping.");
try { //子线程休眠20秒,等待被打断
TimeUnit.SECONDS.sleep(20);
System.out.println("The thread is wake up");
} catch (InterruptedException e) { System.out.println("The thread is interrupted");
} //此处会继续执行下去
System.out.println("The thread is terminal");
} public static void main(String[] args) throws InterruptedException { Thread t = new Thread(new InterruptDemo());
t.start(); System.out.println("The Main is sleeping to wait the thread start!");
//主线程休眠2秒,等待子线程运行
TimeUnit.SECONDS.sleep(2); System.out.println("The thread would be interrupted");
t.interrupt();
}
}
结果
特别要注意的是标红的地方:如果只是单纯的调用interrupt()方法,线程并没有实际被中断,会继续往下执行。
使用isInterrupted()方法判断中断状态
可以在Thread对象上调用isInterrupted()方法来检查任何线程的中断状态。这里需要注意:线程一旦被中断,isInterrupted()方法便会返回true,而一旦sleep()方法抛出异常,它将清空中断标志,此时isInterrupted()方法将返回false。
package com.csdhsm.concurrent; import java.util.concurrent.TimeUnit; /**
* @Title: InterruptedCheck.java
* @Package: com.csdhsm.concurrent
* @Description Interrupted 测试
* @author Han
* @date 2016-4-18 下午7:44:12
* @version V1.0
*/ public class InterruptedCheck { public static void main(String[] args) { Thread t = Thread.currentThread(); System.out.println("Current Thread`s statusA is " + t.isInterrupted());
//自己中断自己~
t.interrupt();
System.out.println("Current Thread`s statusC is " + t.isInterrupted());
System.out.println("Current Thread`s statusB is " + t.isInterrupted()); try { TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) { System.out.println("Current Thread`s statusD is " + t.isInterrupted());
} System.out.println("Current Thread`s statusE is " + t.isInterrupted());
}
}
结果
使用interrupted()方法判断中断状态
可以使用Thread.interrupted()方法来检查当前线程的中断状态(并隐式重置为false)。又由于它是静态方法,因此不能在特定的线程上使用,而只能报告调用它的线程的中断状态,如果线程被中断,而且中断状态尚不清楚,那么,这个方法返回true。与isInterrupted()不同,它将自动重置中断状态为false,第二次调用Thread.interrupted()方法,总是返回false,除非中断了线程。
package com.csdhsm.concurrent; /**
* @Title: InterruptedCheck.java
* @Package: com.csdhsm.concurrent
* @Description Interrupted 测试
* @author Han
* @date 2016-4-18 下午7:44:12
* @version V1.0
*/ public class InterruptedCheck { public static void main(String[] args) { Thread t = Thread.currentThread(); System.out.println("Current Thread`s statusA is " + t.interrupted());
//自己中断自己~
t.interrupt();
System.out.println("Current Thread`s statusC is " + t.interrupted());
System.out.println("Current Thread`s statusB is " + t.interrupted());
System.out.println("Current Thread`s statusD is " + t.interrupted());
}
}
结果
注意看红色的标记
Java并发编程(二)线程任务的中断(interrupt)的更多相关文章
- Java并发编程:线程池的使用
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- Java并发编程二三事
Java并发编程二三事 转自我的Github 近日重新翻了一下<Java Concurrency in Practice>故以此文记之. 我觉得Java的并发可以从下面三个点去理解: * ...
- Java并发编程:线程池的使用(转)
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- Java并发编程:线程池的使用(转载)
转载自:https://www.cnblogs.com/dolphin0520/p/3932921.html Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实 ...
- Java并发编程:线程池的使用(转载)
文章出处:http://www.cnblogs.com/dolphin0520/p/3932921.html Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实 ...
- [转]Java并发编程:线程池的使用
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- 【转】Java并发编程:线程池的使用
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- 13、Java并发编程:线程池的使用
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
Java并发编程:线程间协作的两种方式:wait.notify.notifyAll和Condition 在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者 ...
- Java并发编程:线程控制
在上一篇文章中(Java并发编程:线程的基本状态)我们介绍了线程状态的 5 种基本状态以及线程的声明周期.这篇文章将深入讲解Java如何对线程进行状态控制,比如:如何将一个线程从一个状态转到另一个状态 ...
随机推荐
- Android开发面试经——2.常见Android基础笔试题
标签: androidAndroid基础Android面试题Android笔试题 2015-03-12 15:04 3361人阅读 评论(3) 收藏 举报 分类: Android开发(29) 版 ...
- maven编译项目时提示:cached in the local repository
今天使用命令mvn compile编译maven项目时提示错误信息,部分错误信息如下: ...... was cached in the local repository, resolution wi ...
- D3.js 弦图的制作
这是一种用于描述节点之间联系的图表. 1. 弦图是什么 弦图(Chord),主要用于表示两个节点之间的联系. 两点之间的连线,表示谁和谁具有联系: 线的粗细表示权重: 2. 数据 初始数据为: var ...
- java源程序---可执行文件(.exe)----安装包
众所周知,java源程序都可以导出为(executable jar file)可执行的.jar文件,但是该.jar文件需要在jre环境下才能执行. 那么如何能在没有装jre的电脑上运行呢?(那就要顺带 ...
- 自己学习smarty的一些代码 和记录
http://www.yiibai.com/smarty/smarty_install.html 手册可以看这里 index.tpl <!DOCTYPE html> <html&g ...
- JSON对象如何转化为字符串?
序列化 定义 指将 JavaScript 值转化为 JSON 字符串的过程. JSON.stringify() 能够将 JavaScript 值转换成 JSON 字符串.JSON.stringify( ...
- OpenCV3编程入门笔记(5)重要章节小节及核心函数
- 关于ipxe启动的几个疑问
关于ipxe启动的几个疑问 http://bbs.wuyou.net/forum.php?mod=viewthread&tid=373026&page=1&extra=#pid ...
- VB6 GDI+ 入门教程[2] GDI+初始化
http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[2] GDI+初始化 2009 年 6 月 18 日 7 ...
- struts2后台返回json到jsp页面
1.在action定义一个全局变量如: private Map<String, Object> dataMap; 2.控制层方法 说明:主要的目的是把我们定义的Map转为Json对象,然后 ...