[TOC]
Thread類(lèi)的interrupt方法倒是見(jiàn)過(guò)好幾次课竣,但是一直不知道他的具體是干什么的嗽上,今天查了以下api和core java大概解決了自己的疑惑愕提。(順便吐槽一下core java的翻譯呻惕,問(wèn)題實(shí)在不少)
一专酗、關(guān)于interrupt state
There is boolean flag in every thread that indicating the interrupt state,When the interrupt method is called on a thread, the interrupted status of the thread is set. --core java
每個(gè)線(xiàn)程都有一個(gè)布爾變量表示線(xiàn)程的中斷狀態(tài),當(dāng)線(xiàn)程調(diào)用interrupt后鳞尔,線(xiàn)程的中斷狀態(tài)會(huì)設(shè)置.
另外嬉橙,中斷狀態(tài) 可以通過(guò)線(xiàn)程的isInterrupted()返回值來(lái)判定,true表示設(shè)置為中斷狀態(tài)寥假,false表示非中斷狀態(tài)市框;
The interrupt method can be used to request termination of a thread. --core java
interrupt方法可以請(qǐng)求中斷一個(gè)線(xiàn)程(也就是說(shuō)只是請(qǐng)求中斷,并不能直接中斷).
以下代碼驗(yàn)證了上述結(jié)論糕韧,可以看到程序的輸出會(huì)出現(xiàn)這種情況:首先為false枫振,interrupt以后,變?yōu)閠rue萤彩,但是程序依舊會(huì)輸出0到9粪滤;
private void simpleInterrupt() {
Thread t = new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
}
};
t.start();
System.out.println("before interrupt:" + t.isInterrupted());
t.interrupt();
System.out.println("after interrupt:" + t.isInterrupted());
}
由此可見(jiàn),interrupt并不像它的中文意思中斷一樣能中斷線(xiàn)程雀扶,但是改變了isInterrupted方法的返回值杖小,這個(gè)還是可以作為run方法的循環(huán)結(jié)束的條件,從而中斷線(xiàn)程愚墓,代碼如下:
private void simpleInterruptAffect() {
Thread t = new Thread(){
@Override
public void run() {
for (int i = 0; i < 100 && !isInterrupted(); i++) {
System.out.println(i);
}
}
};
t.start();
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.interrupt();
}
多運(yùn)行幾遍窍侧,發(fā)現(xiàn)i沒(méi)到99就結(jié)束了。
二转绷、sleep伟件、wait等方法和interrupt的聯(lián)系
1. 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. --doc
當(dāng)線(xiàn)程由于sleep或者wait等方法阻塞時(shí),如果調(diào)用了interrupt方法议经,就會(huì)拋出一個(gè)異常(補(bǔ)充:core java上說(shuō)是由于系統(tǒng)無(wú)法檢測(cè)處于阻塞狀態(tài)線(xiàn)程的interrupt state)斧账,并且清除掉線(xiàn)程的interrupt status(使得isInterrupted方法返回false)谴返,用代碼驗(yàn)證一下:
private void interruptSleep() {
Thread t1 = new Thread() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("interrupt status in catch:" + isInterrupted());
}
}
};
t1.start();
System.out.println("interrupt status before interrupt:" + t1.isInterrupted());
t1.interrupt();
System.out.println("interrupt status after interrupt:" + t1.isInterrupted());
}
一般會(huì)依次輸出
interrupt status before interrupt:false
interrupt status after interrupt:true
interrupt status in catch:false
2. 線(xiàn)程不能睡眠的原因
If you call the sleep method when the interrupted status is set, it doesn’t sleep. Instead, it
clears the status and throws an InterruptedException.
如果線(xiàn)程睡眠前線(xiàn)程它的isInterrupted返回true,線(xiàn)程就不會(huì)睡眠咧织,相反嗓袱,線(xiàn)程的會(huì)清除掉interrupt status并且拋出一個(gè)異常。同樣用代碼驗(yàn)證一下习绢。
volatile boolean flag = false;
private void interruptThenSleep() {
Thread t = new Thread(){
@Override
public void run() {
while(!flag){}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("interrupt status in catch:" + isInterrupted());
}
}
};
t.start();
t.interrupt();
System.out.println("interrupt status after interrupt:" + t.isInterrupted());
flag = true;
}
t線(xiàn)程不會(huì)休眠10s
并且輸出:
interrupt status after interrupt:true
interrupt status in catch:false
到這里可也以得出結(jié)論:只要線(xiàn)程的isInterrupted()返回true渠抹,Thread.sleep()都會(huì)拋出異常,并且清除線(xiàn)程的中斷狀態(tài)闪萄。
3.關(guān)于interrupted
Tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method. --doc
檢測(cè)當(dāng)前線(xiàn)程是否處于中斷狀態(tài)梧却,調(diào)用后中斷狀態(tài)會(huì)重置(狀態(tài)變量變?yōu)閒alse)
同樣驗(yàn)證一下
package com.basic;
/**
* Created by liouville on 3/25/16.
*/
public class CompareInterrupt {
volatile static boolean flag = true;
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
System.out.println("主線(xiàn)程:" + t.isInterrupted());
t.interrupt();
flag = false;
}
private static class MyThread extends Thread{
@Override
public void run() {
super.run();
System.out.println("run...");
while (flag){yield();}
System.out.println(isInterrupted());
System.out.println(interrupted());
System.out.println(isInterrupted());
}
}
}
一般輸出:
主線(xiàn)程:false
run...
true
true
false
三、interrupt interrupted isInterrupted
現(xiàn)在可以更清晰的看到三者之間的區(qū)別败去。
interrupt用來(lái)請(qǐng)求中斷線(xiàn)程放航,但是只是設(shè)置一下線(xiàn)程的中斷狀態(tài)的標(biāo)記變量,并不會(huì)直接中斷線(xiàn)程圆裕。
isInterrupted用來(lái)判斷調(diào)用該方法的線(xiàn)程是否處于中斷狀態(tài)广鳍,處于中斷狀態(tài)返回true,否則false;,不會(huì)影響這個(gè)中斷狀態(tài)標(biāo)記變量的值吓妆。
interrupted是一個(gè)靜態(tài)方法赊时,也是用來(lái)檢測(cè)當(dāng)前線(xiàn)程是否處于中斷狀態(tài),處于中斷狀態(tài)返回true,否則false行拢,但是它會(huì)重置中斷狀態(tài)的標(biāo)記變量為false.**