Thread interrupt
来源:互联网 发布:二炮 知乎 编辑:程序博客网 时间:2024/06/01 08:57
http://hapinwater.iteye.com/blog/310558
interrupted
public static boolean interrupted()
- 测试当前线程是否已经中断。线程的中断状态 由该方法清除。换句话说,如果连续两次调用该方法,则第二次调用将返回 false(在第一次调用已清除了其中断状态之后,且第二次调用检验完中断状态前,当前线程再次中断的情况除外)。
线程中断被忽略,因为在中断时不处于活动状态的线程将由此返回 false 的方法反映出来。
- 返回:
- 如果当前线程已经中断,则返回
true
;否则返回false
。 - 另请参见:
isInterrupted()
interrupt
public void interrupt()
- 中断线程。
如果当前线程没有中断它自己(这在任何情况下都是允许的),则该线程的
checkAccess
方法就会被调用,这可能抛出SecurityException
。如果线程在调用
Object
类的wait()
、wait(long)
或wait(long, int)
方法,或者该类的join()
、join(long)
、join(long, int)
、sleep(long)
或sleep(long, int)
方法过程中受阻,则其中断状态将被清除,它还将收到一个InterruptedException
。如果该线程在
可中断的通道
上的 I/O 操作中受阻,则该通道将被关闭,该线程的中断状态将被设置并且该线程将收到一个
ClosedByInterruptException
。如果该线程在一个
Selector
中受阻,则该线程的中断状态将被设置,它将立即从选择操作返回,并可能带有一个非零值,就好像调用了选择器的wakeup
方法一样。如果以前的条件都没有保存,则该线程的中断状态将被设置。
中断一个不处于活动状态的线程不需要任何作用。
- 抛出:
SecurityException
- 如果当前线程无法修改该线程
假如我们有一个任务如下,交给一个Java线程来执行,如何才能保证调用interrupt()来中断它呢?
- class ATask implements Runnable{
- private double d = 0.0;
- public void run() {
- //死循环执行打印"I am running!" 和做消耗时间的浮点计算
- while (true) {
- System.out.println("I am running!");
- for (int i = 0; i < 900000; i++) {
- d = d + (Math.PI + Math.E) / d;
- }
- //给线程调度器可以切换到其它进程的信号
- Thread.yield();
- }
- }
- }
- public class InterruptTaskTest {
- public static void main(String[] args) throws Exception{
- //将任务交给一个线程执行
- Thread t = new Thread(new ATask());
- t.start();
- //运行一断时间中断线程
- Thread.sleep(100);
- System.out.println("****************************");
- System.out.println("Interrupted Thread!");
- System.out.println("****************************");
- t.interrupt();
- }
- }
运行这个程序,我们发现调用interrupt()后,程序仍在运行,如果不强制结束,程序将一直运行下去,如下所示:
- ......
- I am running!
- I am running!
- I am running!
- I am running!
- ****************************
- Interrupted Thread!
- ****************************
- I am running!
- I am running!
- I am running!
- I am running!
- I am running!
- ....
虽然中断发生了,但线程仍然在进行,离开线程有两种常用的方法:
抛出InterruptedException和用Thread.interrupted()检查是否发生中断,下面分别看一下这两种方法:
1.在阻塞操作时如Thread.sleep()时被中断会抛出InterruptedException(注意,进行不能中断的IO操作而阻塞和要获得对象的锁调用对象的synchronized方法而阻塞时不会抛出InterruptedException)
- class ATask implements Runnable{
- private double d = 0.0;
- public void run() {
- //死循环执行打印"I am running!" 和做消耗时间的浮点计算
- try {
- while (true) {
- System.out.println("I am running!");
- for (int i = 0; i < 900000; i++) {
- d = d + (Math.PI + Math.E) / d;
- }
- //休眠一断时间,中断时会抛出InterruptedException
- Thread.sleep(50);
- }
- } catch (InterruptedException e) {
- System.out.println("ATask.run() interrupted!");
- }
- }
- }
程序运行结果如下:
- I am running!
- I am running!
- ****************************
- Interrupted Thread!
- ****************************
- ATask.run() interrupted!
可以看到中断任务时让任务抛出InterruptedException来离开任务.
2.Thread.interrupted()检查是否发生中断.Thread.interrupted()能告诉你线程是否发生中断,并将清除中断状态标记,所以程序不会两次通知你线程发生了中断.
- class ATask implements Runnable{
- private double d = 0.0;
- public void run() {
- //检查程序是否发生中断
- while (!Thread.interrupted()) {
- System.out.println("I am running!");
- for (int i = 0; i < 900000; i++) {
- d = d + (Math.PI + Math.E) / d;
- }
- }
- System.out.println("ATask.run() interrupted!");
- }
- }
程序运行结果如下:
- I am running!
- I am running!
- I am running!
- I am running!
- I am running!
- I am running!
- I am running!
- ****************************
- Interrupted Thread!
- ****************************
- ATask.run() interrupted!
我们可结合使用两种方法来达到可以通过interrupt()中断线程.请看下面例子:
- class ATask implements Runnable{
- private double d = 0.0;
- public void run() {
- try {
- //检查程序是否发生中断
- while (!Thread.interrupted()) {
- System.out.println("I am running!");
- //point1 before sleep
- Thread.sleep(20);
- //point2 after sleep
- System.out.println("Calculating");
- for (int i = 0; i < 900000; i++) {
- d = d + (Math.PI + Math.E) / d;
- }
- }
- } catch (InterruptedException e) {
- System.out.println("Exiting by Exception");
- }
- System.out.println("ATask.run() interrupted!");
- }
- }
在point1之前处point2之后发生中断会产生两种不同的结果,可以通过修改InterruptTaskTest main()里的Thread.sleep()的时间来达到在point1之前产生中断或在point2之后产生中断.
如果在point1之前发生中断,程序会在调用Thread.sleep()时抛出InterruptedException从而结束线程.这和在Thread.sleep()时被中断是一样的效果.程序运行结果可能如下:
- I am running!
- Calculating
- I am running!
- Calculating
- I am running!
- Calculating
- I am running!
- ****************************
- Interrupted Thread!
- ****************************
- Exiting by Exception
- ATask.run() interrupted!
如果在point2之后发生中断,线程会继续执行到下一次while判断中断状态时.程序运行结果可能如下:
- I am running!
- Calculating
- I am running!
- Calculating
- I am running!
- Calculating
- ****************************
- Interrupted Thread!
- ****************************
- ATask.run() interrupted!
0 0
- Thread.interrupt()
- Thread interrupt
- Thread interrupt
- Thread interrupt
- thread.interrupt
- Thread.interrupt之误区
- Thread.interrupt方法
- Thread.interrupt()方法
- Thread.currentThread.interrupt()
- Thread interrupt 方法
- boost thread interrupt
- Thread.interrupt()的使用
- android:Thread.interrupt()
- java Thread interrupt()
- Thread.interrupt与LockSupport
- Thread.currentThread.interrupt()
- Java Thread.interrupt方法
- Thread.interrupt 快速解读
- JetBrains全系列注册机
- 几种常用的优化方法梯度下降法、牛顿法、共轭梯度法
- android 开源项目集合和大牛博客
- LA3942 Remember the Word(字典树+记忆化搜索)
- Node.js-小心buffer的拼接问题
- Thread interrupt
- HASH链地址法和链表插入排序
- android-support-v7包的添加
- svn diff所有历史版本的小脚本
- ubuntu apt-软件安装方式具体用法
- 【HDU1003】【Max Sum】【2种代码】
- uva 307 Sticks
- IAP(程序内购买): 完全攻略
- 同步和信号量