你能解释一下调用java.lang.Thread.interrupt()时做什么吗?


当前回答

为了完整起见,除了其他答案外,如果线程在阻塞Object.wait(..)或thread .sleep(..)等之前被中断,这相当于在阻塞该方法时立即被中断,如下例所示。

public class InterruptTest {
    public static void main(String[] args) {

        Thread.currentThread().interrupt();

        printInterrupted(1);

        Object o = new Object();
        try {
            synchronized (o) {
                printInterrupted(2);
                System.out.printf("A Time %d\n", System.currentTimeMillis());
                o.wait(100);
                System.out.printf("B Time %d\n", System.currentTimeMillis());
            }
        } catch (InterruptedException ie) {
            System.out.printf("WAS interrupted\n");
        }
        System.out.printf("C Time %d\n", System.currentTimeMillis());

        printInterrupted(3);

        Thread.currentThread().interrupt();

        printInterrupted(4);

        try {
            System.out.printf("D Time %d\n", System.currentTimeMillis());
            Thread.sleep(100);
            System.out.printf("E Time %d\n", System.currentTimeMillis());
        } catch (InterruptedException ie) {
            System.out.printf("WAS interrupted\n");
        }
        System.out.printf("F Time %d\n", System.currentTimeMillis());

        printInterrupted(5);

        try {
            System.out.printf("G Time %d\n", System.currentTimeMillis());
            Thread.sleep(100);
            System.out.printf("H Time %d\n", System.currentTimeMillis());
        } catch (InterruptedException ie) {
            System.out.printf("WAS interrupted\n");
        }
        System.out.printf("I Time %d\n", System.currentTimeMillis());

    }
    static void printInterrupted(int n) {
        System.out.printf("(%d) Am I interrupted? %s\n", n,
                Thread.currentThread().isInterrupted() ? "Yes" : "No");
    }
}

输出:

$ javac InterruptTest.java 

$ java -classpath "." InterruptTest
(1) Am I interrupted? Yes
(2) Am I interrupted? Yes
A Time 1399207408543
WAS interrupted
C Time 1399207408543
(3) Am I interrupted? No
(4) Am I interrupted? Yes
D Time 1399207408544
WAS interrupted
F Time 1399207408544
(5) Am I interrupted? No
G Time 1399207408545
H Time 1399207408668
I Time 1399207408669

含义:如果你像下面这样循环,并且中断发生在控制已经离开Thread.sleep(..)并且正在循环的确切时刻,异常仍然会发生。因此,依赖InterruptedException在线程被中断后可靠地抛出是完全安全的:

while (true) {
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
        break;
    }
}

其他回答

如果目标线程一直在等待(通过调用wait(),或其他一些本质上做同样事情的相关方法,如sleep()),它将被中断,这意味着它将停止等待它正在等待的东西,而是接收一个InterruptedException。

完全由线程本身(调用wait()的代码)决定在这种情况下做什么。它不会自动终止线程。

它有时与终止标志结合使用。当被中断时,线程可以检查这个标志,然后关闭自己。但这只是惯例。

Thread.interrupt()方法设置内部的“中断状态”标志。通常该标志由Thread.interrupted()方法检查。

按照惯例,任何通过InterruptedException存在的方法都必须清除中断状态标志。

thread. interrupt()将目标线程的中断状态/标志设置为true,当使用thread. interrupted()检查时,可以帮助停止无休止的线程。请参阅http://www.yegor256.com/2015/10/20/interrupted-exception.html

线程中断基于标志中断状态。 对于每个线程,中断状态的默认值设置为false。 每当在线程上调用interrupt()方法时,中断状态被设置为true。

如果中断状态= true (interrupt()已经在线程上调用), 这个特定的线程不能进入睡眠状态。如果在该线程上调用sleep,则抛出中断异常。再次抛出异常后,标志被设置为false。 如果线程已经处于睡眠状态,并且调用interrupt(),线程将跳出睡眠状态并抛出中断的Exception。

为了完整起见,除了其他答案外,如果线程在阻塞Object.wait(..)或thread .sleep(..)等之前被中断,这相当于在阻塞该方法时立即被中断,如下例所示。

public class InterruptTest {
    public static void main(String[] args) {

        Thread.currentThread().interrupt();

        printInterrupted(1);

        Object o = new Object();
        try {
            synchronized (o) {
                printInterrupted(2);
                System.out.printf("A Time %d\n", System.currentTimeMillis());
                o.wait(100);
                System.out.printf("B Time %d\n", System.currentTimeMillis());
            }
        } catch (InterruptedException ie) {
            System.out.printf("WAS interrupted\n");
        }
        System.out.printf("C Time %d\n", System.currentTimeMillis());

        printInterrupted(3);

        Thread.currentThread().interrupt();

        printInterrupted(4);

        try {
            System.out.printf("D Time %d\n", System.currentTimeMillis());
            Thread.sleep(100);
            System.out.printf("E Time %d\n", System.currentTimeMillis());
        } catch (InterruptedException ie) {
            System.out.printf("WAS interrupted\n");
        }
        System.out.printf("F Time %d\n", System.currentTimeMillis());

        printInterrupted(5);

        try {
            System.out.printf("G Time %d\n", System.currentTimeMillis());
            Thread.sleep(100);
            System.out.printf("H Time %d\n", System.currentTimeMillis());
        } catch (InterruptedException ie) {
            System.out.printf("WAS interrupted\n");
        }
        System.out.printf("I Time %d\n", System.currentTimeMillis());

    }
    static void printInterrupted(int n) {
        System.out.printf("(%d) Am I interrupted? %s\n", n,
                Thread.currentThread().isInterrupted() ? "Yes" : "No");
    }
}

输出:

$ javac InterruptTest.java 

$ java -classpath "." InterruptTest
(1) Am I interrupted? Yes
(2) Am I interrupted? Yes
A Time 1399207408543
WAS interrupted
C Time 1399207408543
(3) Am I interrupted? No
(4) Am I interrupted? Yes
D Time 1399207408544
WAS interrupted
F Time 1399207408544
(5) Am I interrupted? No
G Time 1399207408545
H Time 1399207408668
I Time 1399207408669

含义:如果你像下面这样循环,并且中断发生在控制已经离开Thread.sleep(..)并且正在循环的确切时刻,异常仍然会发生。因此,依赖InterruptedException在线程被中断后可靠地抛出是完全安全的:

while (true) {
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
        break;
    }
}