通常我們會有這樣的需求妓肢,即停止一個線程者疤。在java的api中有stop、suspend等方法可以達到目的阔籽,但由于這些方法在使用上存在不安全性流妻,會帶來不好的副作用,不建議被使用笆制。具體原因可以參考Why is Thread.stop
deprecated绅这。
在本文中,將討論中斷在java中的使用在辆。
中斷在java中主要有3個方法证薇,interrupt(),isInterrupted()和interrupted()。
- interrupt()匆篓,在一個線程中調用另一個線程的interrupt()方法浑度,即會向那個線程發(fā)出信號——線程中斷狀態(tài)已被設置。至于那個線程何去何從鸦概,由具體的代碼實現(xiàn)決定。
- isInterrupted()窗市,用來判斷當前線程的中斷狀態(tài)(true or false)。
- interrupted()是個Thread的static方法咨察,用來恢復中斷狀態(tài),名字起得額??扎拣。
接下來赴肚,看看具體在代碼中如何使用。
interrupt()不能中斷在運行中的線程誉券,它只能改變中斷狀態(tài)而已刊愚。
public class InterruptionInJava implements Runnable{
public static void main(String[] args) throws InterruptedException {
Thread testThread = new Thread(new InterruptionInJava(),"InterruptionInJava");
//start thread
testThread.start();
Thread.sleep(1000);
//interrupt thread
testThread.interrupt();
System.out.println("main end");
}
@Override
public void run() {
while(true){
if(Thread.currentThread().isInterrupted()){
System.out.println("Yes,I am interruted,but I am still running");
}else{
System.out.println("not yet interrupted");
}
}
}
}
結果顯示踊跟,被中斷后,仍舊運行,不停打印Yes,I am interruted,but I am still running
那么商玫,如何正確中斷箕憾?
既然是只能修改中斷狀態(tài)拳昌,那么我們應該針對中斷狀態(tài)做些什么。
public class InterruptionInJava implements Runnable{
public static void main(String[] args) throws InterruptedException {
Thread testThread = new Thread(new InterruptionInJava(),"InterruptionInJava");
//start thread
testThread.start();
// Thread.sleep(1000);
//interrupt thread
testThread.interrupt();
System.out.println("main end");
}
@Override
public void run() {
while(true){
if(Thread.currentThread().isInterrupted()){
System.out.println("Yes,I am interruted,but I am still running");
return;
}else{
System.out.println("not yet interrupted");
}
}
}
}
修改代碼御铃,在狀態(tài)判斷中如上沈矿,添加一個return就okay了。但現(xiàn)實中羹膳,我們可能需要做的更通用,不禁又要發(fā)出天問陵像,如何中斷線程?答案是添加一個開關嗓违。
public class InterruptionInJava implements Runnable{
private volatile static boolean on = false;
public static void main(String[] args) throws InterruptedException {
Thread testThread = new Thread(new InterruptionInJava(),"InterruptionInJava");
//start thread
testThread.start();
Thread.sleep(1000);
InterruptionInJava.on = true;
System.out.println("main end");
}
@Override
public void run() {
while(!on){
if(Thread.currentThread().isInterrupted()){
System.out.println("Yes,I am interruted,but I am still running");
}else{
System.out.println("not yet interrupted");
}
}
}
}
會輸出類似結果图贸,這表明是成功中斷了的:
這種開關的方式看起來包治百病冕广,但是當遇到線程阻塞時,就會很無奈了沟优,正如下面代碼所示:
public class InterruptionInJava implements Runnable{
private volatile static boolean on = false;
public static void main(String[] args) throws InterruptedException {
Thread testThread = new Thread(new InterruptionInJava(),"InterruptionInJava");
//start thread
testThread.start();
Thread.sleep(1000);
InterruptionInJava.on = true;
System.out.println("main end");
}
@Override
public void run() {
while(!on){
try {
Thread.sleep(10000000);
} catch (InterruptedException e) {
System.out.println("caught exception: "+e);
}
}
}
}
線程被阻塞無法被中斷睬辐。這時候救世主interrupt函數(shù)又回來了,它可以迅速中斷被阻塞的線程溯饵,拋出一個InterruptedException,把線程從阻塞狀態(tài)中解救出來,show the code丰刊。
public class InterruptionInJava implements Runnable{
private volatile static boolean on = false;
public static void main(String[] args) throws InterruptedException {
Thread testThread = new Thread(new InterruptionInJava(),"InterruptionInJava");
//start thread
testThread.start();
Thread.sleep(1000);
InterruptionInJava.on = true;
testThread.interrupt();
System.out.println("main end");
}
@Override
public void run() {
while(!on){
try {
Thread.sleep(10000000);
} catch (InterruptedException e) {
System.out.println("caught exception right now: "+e);
}
}
}
}
結果截圖隘谣,達到預期啄巧。
這種情形同樣適用io阻塞,通常io阻塞會立即拋出一個SocketException码泛,類似于上面說的InterruptedException。