---
title: "interrupted()和isInterrupted()的区别"
author: "fairy_xzc"
date: "2017-03-02T16:29:16Z"
tags: []
categories: ["编程语言"]
slug: "interrupted-isinterrupted"
draft: false
lastmod: "2017-03-02T16:29:50Z"
id: 2359893
url: "https://agilestyle.iteye.com/blog/2359893"
---

**原创转载请注明出处：[http://agilestyle.iteye.com/blog/2359893](/blog/2359893)**

 

## Method in Java Doc

![](http://dl2.iteye.com/upload/attachment/0123/4405/5212742b-aa44-3ef5-9775-c9642616f10e.png)  
 

调用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

![](http://dl2.iteye.com/upload/attachment/0123/4407/68836205-f74a-3e0a-b2ab-debd41a4da24.png)  
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

![](http://dl2.iteye.com/upload/attachment/0123/4411/977f4240-f755-3508-8e92-b6147ef6f7b4.png)  
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

![](http://dl2.iteye.com/upload/attachment/0123/4415/c8042512-c074-3a7b-ba57-6eed1828de47.png)  
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

![](http://dl2.iteye.com/upload/attachment/0123/4421/6b791855-d7c0-354b-a97e-d3e3fc7701a6.png)  
is interrupted 1: true 

![](http://dl2.iteye.com/upload/attachment/0123/4425/bd0b310d-5741-3ccc-aa49-cd0497552d27.png)  
is interrupted 2: true

![](http://dl2.iteye.com/upload/attachment/0123/4427/43ce8f64-f1ac-39b4-9c30-610f2bc6577b.png)  
Note：

isInterrupted()并未清除状态flag，所以打印两个true

 

## Summary

interrupted(): 测试当前线程是否已经是中断状态，执行后具有清除中断状态flag的功能

isInterrupted(): 测试线程Thread对象是否已经是中断状态，但不清除中断状态flag 

 

## Reference

Java多线程编程核心技术 高洪岩 注