原创转载请注明出处:http://agilestyle.iteye.com/blog/2359893
Method in Java Doc
调用interrupt()方法仅仅是在当前线程中打了一个停止的标记,并不是真正的停止线程
package org.fool.java.concurrent.interrupt; public class InterruptTest1 { public static void main(String[] args) { try { Thread thread = new Thread(new MyThread()); thread.start(); Thread.sleep(5000); thread.interrupt(); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } } public static class MyThread implements Runnable { @Override public void run() { for (int i = 0; i < 200000; i++) { System.out.println("i = " + (i + 1)); } } } }
Console Output
Note:
从运行结果来看,调用interrupt方法并没有停止线程
interrupted()
测试当前线程是否已经中断
package org.fool.java.concurrent.interrupt; public class InterruptTest2 { public static void main(String[] args) { try { Thread thread = new Thread(new MyThread()); thread.start(); Thread.sleep(1000); thread.interrupt(); System.out.println("is interrupted 1: " + thread.interrupted()); System.out.println("is interrupted 2: " + thread.interrupted()); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } } public static class MyThread implements Runnable { @Override public void run() { for (int i = 0; i < 50000; i++) { System.out.println("i = " + (i + 1)); } } } }
Console Output
Note:
从控制台打印的结果来看,线程并未停止,这也证明了interrupted()方法的解释:测试当前线程是否已经中断。本例中这个“当前线程”是main,它从未中断过,所以打印的结果是两个false
修改Main函数
package org.fool.java.concurrent.interrupt; public class InterruptTest3 { public static void main(String[] args) { try { Thread thread = new Thread(new MyThread()); thread.start(); Thread.sleep(1000); Thread.currentThread().interrupt(); System.out.println(Thread.currentThread().getName()); System.out.println("is interrupted 1: " + Thread.currentThread().interrupted()); System.out.println("is interrupted 2: " + Thread.currentThread().interrupted()); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } } public static class MyThread implements Runnable { @Override public void run() { for (int i = 0; i < 50000; i++) { System.out.println("i = " + (i + 1)); } } } }
Console Output
Note:
控制台第一次打印的结果是true,第二次为false;Java Doc中给出的解释是:测试当前线程是否已经中断,线程的中断状态由该方法清除。即如果连续两次调用该方法,则第二次调用将返回false(在第一次调用已清除flag后以及第二次调用检查中断状态之前,当前线程再次中断的情况除外)
所以,interrupted()方法具有清除状态flag的功能
isInterrupted()
测试线程是否已经中断
package org.fool.java.concurrent.interrupt; public class InterruptTest4 { public static void main(String[] args) { try { Thread thread = new Thread(new MyThread()); thread.start(); Thread.sleep(1000); thread.interrupt(); System.out.println("is interrupted 1: " + thread.isInterrupted()); System.out.println("is interrupted 2: " + thread.isInterrupted()); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } } public static class MyThread implements Runnable { @Override public void run() { for (int i = 0; i < 200000; i++) { System.out.println("i = " + (i + 1)); } } } }
Console Output
is interrupted 1: true
is interrupted 2: true
Note:
isInterrupted()并未清除状态flag,所以打印两个true
Summary
interrupted(): 测试当前线程是否已经是中断状态,执行后具有清除中断状态flag的功能
isInterrupted(): 测试线程Thread对象是否已经是中断状态,但不清除中断状态flag
Reference
Java多线程编程核心技术 高洪岩 注
相关推荐
主要介绍了JAVA多线程之中断机制stop()、interrupted()、isInterrupted()的相关资料,需要的朋友可以参考下
interrupt,interrupted,isInterrupted方法区别 join方法 yield方法 多线程 进程和线程 进程与线程最主要的区别是它们是操作系统管理资源的不同方式的体现。 准确来说进程与线程属于衍生关系。 进程是操作系统执行...
一般来说,支持线程中断的方法(也就是线程中断后会抛出InterruptedException的方法),阻塞函数,如:Thread.sleep、Thread.jo
线程中断 常见的有以下两种方式: 通过共享的标记来进行沟通 调用 interrupt() 方法来通知 通过共享的标记来实现中断 就是创建一个boolean类型的变量来控制循环是否进行,就是一个标记。 代码如下: ...
java线程中的interrupt,isInterrupt,interrupted方法
interrupted与isInterrupted的区别: interrupted是Thread类的静态方法,里面调用了isTnterrupted方法[currentThread().isInterrupted()],测试当前线程是否已经中断,线程的中断状态由该方法清除 isInterrupted是Thread...
错误现象:(semop函数调用,strerror(errno)输出结果)Interrupted system call平台:RedHat LinuxLINUX文档关于EINTR的描述是这样子的: While blocked in this system call, the process caught a signal.UNIX文档...
中断在java中主要有3个方法,interrupt(),isInterrupted()和interrupted()。 interrupt(),在一个线程中调用另一个线程的interrupt()方法,即会向那个线程发出信号——线程中断状态已被设置。至于那个线程何去何从...
解决ntbst启动有弹出框.reg
OSError:[Errno 4]Interrupted system call 问题描述: File “/anaconda2/python2.7/multiprocessing/reduction.py”, line 157, in rebuild_handle new_handle = recv_handle(conn) File “/anaconda2/python2.7/...
oracle连接失败 解决方案 超详细 挺有用的
Previous operation has not finished; run 'cleanup' if it was interrupted. sqlite3免费百度云下载,亲测可用!-附件资源
########## # README # ########## Purpose ------- To help understand which is the most commonly 'pressed' and 'released' key in the keyboard....of pressing need not be equal to release count....
Git安装包及使用教程,有window版本的安装包,也有Linux安装包,特别适合初学者学习git相关命令和操作。
1、把sqlite3.exe放在SVN项目.svn文件夹下,如果隐藏了请显示 2、在.svn目录下打开cnd 3、执行sqlite3 wc.db 4、执行.table 查看是否有work_queue表,没有关闭cmd重新执行2.3 5、执行delete from work_queue;...
NULL 博文链接:https://zzc1684.iteye.com/blog/2305849
Efficient filter design against interrupted sampling repeater jamming for wideband radar
(4)start和run:不能直接调用run方法,因为这样并不会启动一个新的线程,依旧是在main线程来执行。相当于普通方法的直接调用 2.常见方法: (1)Sleep:写的位置决定作用于哪个线程,其他线程可以通过interrupt来打断...
Preivious operation has not finished; run 'cleanup' if it was interrupted Please execute the 'Cleanup' command 问题解决 sqlite3 .svn/wc.db "delete from work_queue