关于Thread类中三个interrupt方法的研究与学习(转)
先看三个方法原型:
public void interrupt();
public boolean isInterrupted();
public static boolean interrupted();
一、先说interrupt()方法,看注释
Interrupts this thread.
Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a java.nio.channels.ClosedByInterruptException.
If this thread is blocked in a java.nio.channels.Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.
If none of the previous conditions hold then this thread's interrupt status will be set.
Interrupting a thread that is not alive need not have any effect.
Throws:
SecurityException - if the current thread cannot modify this thread
意思是说,当这个线程刚好或即将被阻塞在wait,join,sleep方法的时候,调用这个方法会引起这个线程的interrupt状态被清空(设为false),并且前者三个方法会抛出InterruptedException。
除此之外(这个线程不处于wait,join,sleep方法),这个线程的interrupt状态会被设置(设为true)。
二、isInterrupted()方法,看源码:
- public boolean isInterrupted() {
- return isInterrupted(false);
- }
- private native boolean isInterrupted(boolean ClearInterrupted);
看注释:
Tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method.
意思是说:返回这个线程是否被interrupt了,调用这个方法不会影响这个线程的interrupt状态。
三、public static boolean interrupted();看源码:
- public static boolean interrupted() {
- return currentThread().isInterrupted(true);
- }
- private native boolean isInterrupted(boolean ClearInterrupted);
看注释:
Tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method.
In other words, if this method were to be called twice in succession, the second call would return false (unless the current thread were interrupted again, after the first call had cleared its interrupted status and before the second call had examined it).
意思是说:调用这个方法会返回当前线程的interrupt状态(true或false),并把当前线程的interrupt状态清空(设为false)。
注意:这个是个静态方法,并且返回的是当前线程状态,并不一定是调用者的线程状态。
看例子:
例一:
- @Test
- public void test1(){
- Thread t1= new Thread(){
- @Override
- public void run() {
- System.out.println("begin");
- for(int i=0;i<100;i++){
- System.out.println("i="+i+" "+this.isInterrupted());
- try {
- Socket socket= new Socket("10.22.1.115",23);//不存在的ip,让这句话执行时间长一些,方便看效果
- } catch (UnknownHostException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- System.out.println("i="+i+" this.isInterrupted():"+this.isInterrupted());
- System.out.println("i="+i+" Thread.interrupted():"+Thread.interrupted());
- System.out.println("i="+i+" this.isInterrupted():"+this.isInterrupted());
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- // e.printStackTrace();
- System.out.println("exception:"+this.isInterrupted());
- System.out.println("exception:"+Thread.interrupted());
- // return;
- }
- }
- System.out.println("end");
- }
- };
- t1.start();
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(t1.interrupted()+" "+Thread.interrupted());
- System.out.println("begin interrupt t1.isInterrupted():"+t1.isInterrupted());
- t1.interrupt();
- System.out.println(t1.interrupted()+" "+Thread.interrupted());
- System.out.println("end interrupt t1.isInterrupted():"+t1.isInterrupted());
- System.out.println(t1.interrupted()+" "+Thread.interrupted());
- try {
- Thread.sleep(100000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
执行结果:
begin
i=0 false
false false
begin interrupt t1.isInterrupted():false
false false
end interrupt t1.isInterrupted():true
false false
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at baby.thread.InterruptTest$1.run(InterruptTest.java:21)
i=0 this.isInterrupted():true
i=0 Thread.interrupted():true
i=0 this.isInterrupted():false
休息5秒钟
i=1 false
…………
例二:注释掉Thread.interrupted()一行
- @Test
- public void test13(){
- Thread t1= new Thread(){
- @Override
- public void run() {
- System.out.println("begin");
- for(int i=0;i<100;i++){
- System.out.println("i="+i+" "+this.isInterrupted());
- try {
- Socket socket= new Socket("10.22.1.115",23);
- } catch (UnknownHostException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- System.out.println("i="+i+" this.isInterrupted():"+this.isInterrupted());
- // System.out.println("i="+i+" Thread.interrupted():"+Thread.interrupted());
- System.out.println("i="+i+" this.isInterrupted():"+this.isInterrupted());
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- // e.printStackTrace();
- System.out.println("exception:"+this.isInterrupted());
- System.out.println("exception:"+Thread.interrupted());
- // return;
- }
- }
- System.out.println("end");
- }
- };
- t1.start();
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(t1.interrupted()+" "+Thread.interrupted());
- System.out.println("begin interrupt t1.isInterrupted():"+t1.isInterrupted());
- t1.interrupt();
- System.out.println(t1.interrupted()+" "+Thread.interrupted());
- System.out.println("end interrupt t1.isInterrupted():"+t1.isInterrupted());
- System.out.println(t1.interrupted()+" "+Thread.interrupted());
- try {
- Thread.sleep(100000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
输出:
begin
i=0 false
false false
begin interrupt t1.isInterrupted():false
false false
end interrupt t1.isInterrupted():true
false false
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at baby.thread.InterruptTest$2.run(InterruptTest.java:82)
i=0 this.isInterrupted():true
i=0 this.isInterrupted():true
没有休息5秒,直接下面输出
exception:false
exception:false
i=1 false
例三:自己中断自己
- @Test
- public void test12(){
- Thread t1= new Thread(){
- @Override
- public void run() {
- System.out.println("begin");
- for(int i=0;i<100;i++){
- System.out.println("i="+i+" "+this.isInterrupted());
- interrupt();
- try {
- Socket socket= new Socket("10.22.1.115",23);
- } catch (UnknownHostException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- System.out.println("i="+i+" this.isInterrupted():"+this.isInterrupted());
- System.out.println("i="+i+" Thread.interrupted():"+Thread.interrupted());
- System.out.println("i="+i+" this.isInterrupted():"+this.isInterrupted());
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- // e.printStackTrace();
- System.out.println("exception:"+this.isInterrupted());
- System.out.println("exception:"+Thread.interrupted());
- // return;
- }
- }
- System.out.println("end");
- }
- };
- t1.start();
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(t1.interrupted()+" "+Thread.interrupted());
- System.out.println("begin interrupt t1.isInterrupted():"+t1.isInterrupted());
- // t1.interrupt();
- System.out.println(t1.interrupted()+" "+Thread.interrupted());
- System.out.println("end interrupt t1.isInterrupted():"+t1.isInterrupted());
- System.out.println(t1.interrupted()+" "+Thread.interrupted());
- try {
- Thread.sleep(100000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
结果输出和例一一样:
begin
i=0 false
false false
begin interrupt t1.isInterrupted():true
false false
end interrupt t1.isInterrupted():true
false false
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at baby.thread.InterruptTest$3.run(InterruptTest.java:143)
i=0 this.isInterrupted():true
i=0 Thread.interrupted():true
i=0 this.isInterrupted():false
休息5秒
i=1 false
例四:中断时处于sleep方法
- @Test
- public void test14(){
- Thread t1= new Thread(){
- @Override
- public void run() {
- System.out.println("begin");
- for(int i=0;i<100;i++){
- System.out.println("i="+i+" "+this.isInterrupted());
- System.out.println("i="+i+" this.isInterrupted():"+this.isInterrupted());
- // System.out.println("i="+i+" Thread.interrupted():"+Thread.interrupted());
- System.out.println("i="+i+" this.isInterrupted():"+this.isInterrupted());
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- // e.printStackTrace();
- System.out.println("exception:"+this.isInterrupted());
- System.out.println("exception:"+Thread.interrupted());
- // return;
- }
- }
- System.out.println("end");
- }
- };
- t1.start();
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // System.out.println(t1.interrupted()+" "+Thread.interrupted());
- System.out.println("begin interrupt t1.isInterrupted():"+t1.isInterrupted());
- t1.interrupt();
- //System.out.println(t1.interrupted()+" "+Thread.interrupted());
- System.out.println("end interrupt t1.isInterrupted():"+t1.isInterrupted());
- // System.out.println(t1.interrupted()+" "+Thread.interrupted());
- try {
- Thread.sleep(100000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
结果输出:
begin
i=0 false
i=0 this.isInterrupted():false
i=0 this.isInterrupted():false
begin interrupt t1.isInterrupted():false
没有休息5秒
exception:false
exception:false
i=1 false
i=1 this.isInterrupted():false
i=1 this.isInterrupted():false
end interrupt t1.isInterrupted():false
http://blog.csdn.net/chaofanwei/article/details/19157747
http://www.cnblogs.com/hanyuan/archive/2013/03/10/2952229.html
http://stackoverflow.com/questions/13623445/future-cancel-method-is-not-working
关于Thread类中三个interrupt方法的研究与学习(转)的更多相关文章
- 【Java_多线程并发编程】基础篇—Thread类中start()和run()方法的区别
1. start() 和 run()的区别说明 start()方法: 它会启动一个新线程,并将其添加到线程池中,待其获得CPU资源时会执行run()方法,start()不能被重复调用. run()方法 ...
- 正确停止线程的方式三 使用Thread类中的内置的中断标记位-----------不熟悉
package charpter10; public class Processor implements Runnable { @Override public void run() { for ( ...
- Java线程状态及Thread类中的主要方法
要想实现多线程,就必须在主线程中创建新的线程对象. 不论什么线程一般具有5种状态,即创建,就绪,执行,堵塞,终止. 创建状态: 在程序中用构造方法创建了一个线程对象后,新的线程对象便处于新建状态,此时 ...
- Thread类中的join方法
package charpter06; //类实现接口public class Processor implements Runnable { // 重写接口方法 @Override public v ...
- 多线程(Thread类中的方法线程名称)
1 package multithread; 2 3 /* 4 * 如何创建一个线程呢? 5 * 6 * 创建线程方式一:继承Thread类. 7 * 8 * 步骤: 9 * 1,定义一个类继承Thr ...
- java 多线程3:Thread类中的静态方法
Thread类中的静态方法 Thread类中的静态方法表示操作的线程是"正在执行静态方法所在的代码块的线程".为什么Thread类中要有静态方法,这样就能对CPU当前正在运行的线程 ...
- UnSafe类中的一些重要方法
UnSafe类中的一些重要方法 JDK中的rt.jar保重Unsafe类中提供了硬件级别的原子性操作,Unsafe类中的方法都是navtice方法,他们使用JNI的方式访问C++实现库,下面我们来了解 ...
- Python之路(第四十二篇)线程相关的其他方法、join()、Thread类的start()和run()方法的区别、守护线程
一.线程相关的其他方法 Thread实例对象的方法 # isAlive(): 返回线程是否活动的. # getName(): 返回线程名. # setName(): 设置线程名. threadin ...
- python: 面向对象:类和对象调用类中的变量和方法
一. 面向对象初识 我们在生活中做事都是面向过程的,前面实现一些基本逻辑功能代码也是用面向过程的语句实现的,后来学了函数,把这些功能又装到了函数里.但用面向过程的方法去写程序,只能实现一个功能,我们要 ...
随机推荐
- java io学习记录(路径分隔符)
java路径分隔符(路径表示) path="E:\\xp\\test\\2.jpg"; path="E:/xp/test/2.jpg"; path=" ...
- Python 第五篇(上):算法、自定义模块、系统标准模块(time 、datetime 、random 、OS 、sys 、hashlib 、json和pickle)
一:算法回顾: 冒泡算法,也叫冒泡排序,其特点如下: 1.比较相邻的元素.如果第一个比第二个大,就交换他们两个. 2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应 ...
- IOS常用设计模式之委托模式
对于iOS开发,举例Cocoa框架下的几个设计模式为大家分析.当然,Cocoa框架下关于设计模式的内容远远不止这些,我们选择了常用的几种:单例模式.委托模式.观察者模式.MVC模式. 委托模式 委托模 ...
- 02-IOSCore - NSFileHandle、合并文件、文件指针、文件查看器
[day0201_NSFileHandle]:文件句柄 1 NSFileHandle 文件对接器.文件句柄 常用API: - (NSData *)readDataToEndOfFile;读取数据到最后 ...
- VS2013 Qt5 Mysql中文编码问题
Qt开始默认是utf-8,而VS2013默认程序编码为gb2312: 这样就会发现使用中文的时候乱码. 一般有二种解决方案: 1.在使用中文的时候,使用QTextCodec QTextCodec *g ...
- JVM调优总结(六)-分代垃圾回收详述2
分代垃圾回收流程示意 选择合适的垃圾收集算法 串行收集器 用单线程处理所有垃圾回收工作,因为无需多线程交互,所以效率比较高.但是,也无法使用多处理器的优势,所以此收集器适合单处理器机器.当然,此收集器 ...
- Android 富文本框实现 RichEditText
Android系统自带控件没有富文本框控件,如果想写一封带格式的邮件基本上不可能,EdtiText只有默认一种格式,显示不能滿足要求,!!正好项目需要研究了一下,开发了此控件,现将一些源代码开放一下, ...
- Android 之SparseArray<E>详解
SparseArray是android里为<Interger,Object>这样的Hashmap而专门写的class,目的是提高效率,其核心是折半查找函数(binarySearch) pr ...
- 删掉SafeDrv病毒(这个病毒有点意思)
1.手动删除以下文件: %program files%\common files\safedrv.exe %documents and settings%\administrator\rkoxe.dr ...
- 杭电ACM1408——盐水的故事
简单的题目,RT,就能够写出代码.须要注意的是类型的应用,应该用浮点型. 以下的是AC的代码: #include <iostream> using namespace std; int m ...