Java interrupt()方法使用注意_動力節(jié)點Java學(xué)院整理
程序是很簡易的。然而,在編程人員面前,多線程呈現(xiàn)出了一組新的難題,如果沒有被恰當?shù)慕鉀Q,將導(dǎo)致意外的行為以及細微的、難以發(fā)現(xiàn)的錯誤。
在本篇文章中,我們針對這些難題之一:如何中斷一個正在運行的線程。
背景
中斷(Interrupt)一個線程意味著在該線程完成任務(wù)之前停止其正在進行的一切,有效地中止其當前的操作。線程是死亡、還是等待新的任務(wù)或是繼續(xù)運行至下一步,就取決于這個程序。雖然初次看來它可能顯得簡單,但是,你必須進行一些預(yù)警以實現(xiàn)期望的結(jié)果。你最好還是牢記以下的幾點告誡。
首先,忘掉Thread.stop方法。雖然它確實停止了一個正在運行的線程,然而,這種方法是不安全也是不受提倡的,這意味著,在未來的Java版本中,它將不復(fù)存在。
一些輕率的家伙可能被另一種方法Thread.interrupt所迷惑。盡管,其名稱似乎在暗示著什么,然而,這種方法并不會中斷一個正在運行的線程(待會將進一步說明),正如Listing A中描述的那樣。它創(chuàng)建了一個線程,并且試圖使用Thread.interrupt方法停止該線程。Thread.sleep()方法的調(diào)用,為線程的初始化和中止提供了充裕的時間。線程本身并不參與任何有用的操作。
Listing A
class Example1 extends Thread {
boolean stop=false;
public static void main( String args[] ) throws Exception {
Example1 thread = new Example1();
System.out.println( "Starting thread..." );
thread.start();
Thread.sleep( 3000 );
System.out.println( "Interrupting thread..." );
thread.interrupt();
Thread.sleep( 3000 );
System.out.println("Stopping application..." );
//System.exit(0);
}
public void run() {
while(!stop){
System.out.println( "Thread is running..." );
long time = System.currentTimeMillis();
while((System.currentTimeMillis()-time < 1000)) {
}
}
System.out.println("Thread exiting under request..." );
}
}
如果你運行了Listing A中的代碼,你將在控制臺看到以下輸出:
Starting thread...
Thread is running...
Thread is running...
Thread is running...
Interrupting thread...
Thread is running...
Thread is running...
Thread is running...
Stopping application...
Thread is running...
Thread is running...
Thread is running...
...............................
甚至,在Thread.interrupt()被調(diào)用后,線程仍然繼續(xù)運行。
真正地中斷一個線程
中斷線程最好的,最受推薦的方式是,使用共享變量(shared variable)發(fā)出信號,告訴線程必須停止正在運行的任務(wù)。線程必須周期性的核查這一變量(尤其在冗余操作期間),然后有秩序地中止任務(wù)。Listing B描述了這一方式。
Listing B
class Example2 extends Thread {
volatile boolean stop = false;
public static void main( String args[] ) throws Exception {
Example2 thread = new Example2();
System.out.println( "Starting thread..." );
thread.start();
Thread.sleep( 3000 );
System.out.println( "Asking thread to stop..." );
thread.stop = true;
Thread.sleep( 3000 );
System.out.println( "Stopping application..." );
//System.exit( 0 );
}
public void run() {
while ( !stop ) {
System.out.println( "Thread is running..." );
long time = System.currentTimeMillis();
while ( (System.currentTimeMillis()-time < 1000) && (!stop) ) {
}
}
System.out.println( "Thread exiting under request..." );
}
}
運行Listing B中的代碼將產(chǎn)生如下輸出(注意線程是如何有秩序的退出的)
Starting thread...
Thread is running...
Thread is running...
Thread is running...
Asking thread to stop...
Thread exiting under request...
Stopping application...
雖然該方法要求一些編碼,但并不難實現(xiàn)。同時,它給予線程機會進行必要的清理工作,這在任何一個多線程應(yīng)用程序中都是絕對需要的。請確認將共享變量定義成volatile 類型或?qū)λ囊磺性L問封入同步的塊/方法(synchronized blocks/methods)中。
到目前為止一切順利!但是,當線程等待某些事件發(fā)生而被阻塞,又會發(fā)生什么?當然,如果線程被阻塞,它便不能核查共享變量,也就不能停止。這在許多情況下會發(fā)生,例如調(diào)用Object.wait()、ServerSocket.accept()和DatagramSocket.receive()時,這里僅舉出一些。
他們都可能永久的阻塞線程。即使發(fā)生超時,在超時期滿之前持續(xù)等待也是不可行和不適當?shù)?,所以,要使用某種機制使得線程更早地退出被阻塞的狀態(tài)。
很不幸運,不存在這樣一種機制對所有的情況都適用,但是,根據(jù)情況不同卻可以使用特定的技術(shù)。在下面的環(huán)節(jié),我將解答一下最普遍的例子。
使用Thread.interrupt()中斷線程
正如Listing A中所描述的,Thread.interrupt()方法不會中斷一個正在運行的線程。這一方法實際上完成的是,在線程受到阻塞時拋出一個中斷信號,這樣線程就得以退出阻塞的狀態(tài)。更確切的說,如果線程被Object.wait, Thread.join和Thread.sleep三種方法之一阻塞,那么,它將接收到一個中斷異常(InterruptedException),從而提早地終結(jié)被阻塞狀態(tài)。
因此,如果線程被上述幾種方法阻塞,正確的停止線程方式是設(shè)置共享變量,并調(diào)用interrupt()(注意變量應(yīng)該先設(shè)置)。如果線程沒有被阻塞,這時調(diào)用interrupt()將不起作用;否則,線程就將得到異常(該線程必須事先預(yù)備好處理此狀況),接著逃離阻塞狀態(tài)。在任何一種情況中,最后線程都將檢查共享變量然后再停止。Listing C這個示例描述了該技術(shù)。
Listing C
class Example3 extends Thread {
volatile boolean stop = false;
public static void main( String args[] ) throws Exception {
Example3 thread = new Example3();
System.out.println( "Starting thread..." );
thread.start();
Thread.sleep( 3000 );
System.out.println( "Asking thread to stop..." );
thread.stop = true;//如果線程阻塞,將不會檢查此變量
thread.interrupt();
Thread.sleep( 3000 );
System.out.println( "Stopping application..." );
//System.exit( 0 );
}
public void run() {
while ( !stop ) {
System.out.println( "Thread running..." );
try {
Thread.sleep( 1000 );
} catch ( InterruptedException e ) {
System.out.println( "Thread interrupted..." );
}
}
System.out.println( "Thread exiting under request..." );
}
}
一旦Listing C中的Thread.interrupt()被調(diào)用,線程便收到一個異常,于是逃離了阻塞狀態(tài)并確定應(yīng)該停止。運行以上代碼將得到下面的輸出:
Starting thread...
Thread running...
Thread running...
Thread running...
Asking thread to stop...
Thread interrupted...
Thread exiting under request...
Stopping application...
中斷I/O操作
然而,如果線程在I/O操作進行時被阻塞,又會如何?I/O操作可以阻塞線程一段相當長的時間,特別是牽扯到網(wǎng)絡(luò)應(yīng)用時。例如,服務(wù)器可能需要等待一個請求(request),又或者,一個網(wǎng)絡(luò)應(yīng)用程序可能要等待遠端主機的響應(yīng)。
如果你正使用通道(channels)(這是在Java 1.4中引入的新的I/O API),那么被阻塞的線程將收到一個ClosedByInterruptException異常。如果情況是這樣,其代碼的邏輯和第三個例子中的是一樣的,只是異常不同而已。
但是,你可能正使用Java1.0之前就存在的傳統(tǒng)的I/O,而且要求更多的工作。既然這樣,Thread.interrupt()將不起作用,因為線程將不會退出被阻塞狀態(tài)。Listing D描述了這一行為。盡管interrupt()被調(diào)用,線程也不會退出被阻塞狀態(tài)
Listing D
import java.io.*;
class Example4 extends Thread {
public static void main( String args[] ) throws Exception {
Example4 thread = new Example4();
System.out.println( "Starting thread..." );
thread.start();
Thread.sleep( 3000 );
System.out.println( "Interrupting thread..." );
thread.interrupt();
Thread.sleep( 3000 );
System.out.println( "Stopping application..." );
//System.exit( 0 );
}
public void run() {
ServerSocket socket;
try {
socket = new ServerSocket(7856);
} catch ( IOException e ) {
System.out.println( "Could not create the socket..." );
return;
}
while ( true ) {
System.out.println( "Waiting for connection..." );
try {
Socket sock = socket.accept();
} catch ( IOException e ) {
System.out.println( "accept() failed or interrupted..." );
}
}
}
}
很幸運,Java平臺為這種情形提供了一項解決方案,即調(diào)用阻塞該線程的套接字的close()方法。在這種情形下,如果線程被I/O操作阻塞,該線程將接收到一個SocketException異常,這與使用interrupt()方法引起一個InterruptedException異常被拋出非常相似。
唯一要說明的是,必須存在socket的引用(reference),只有這樣close()方法才能被調(diào)用。這意味著socket對象必須被共享。Listing E描述了這一情形。運行邏輯和以前的示例是相同的。
Listing E
import java.net.*;
import java.io.*;
class Example5 extends Thread {
volatile boolean stop = false;
volatile ServerSocket socket;
public static void main( String args[] ) throws Exception {
Example5 thread = new Example5();
System.out.println( "Starting thread..." );
thread.start();
Thread.sleep( 3000 );
System.out.println( "Asking thread to stop..." );
thread.stop = true;
thread.socket.close();
Thread.sleep( 3000 );
System.out.println( "Stopping application..." );
//System.exit( 0 );
}
public void run() {
try {
socket = new ServerSocket(7856);
} catch ( IOException e ) {
System.out.println( "Could not create the socket..." );
return;
}
while ( !stop ) {
System.out.println( "Waiting for connection..." );
try {
Socket sock = socket.accept();
} catch ( IOException e ) {
System.out.println( "accept() failed or interrupted..." );
}
}
System.out.println( "Thread exiting under request..." );
}
}
以下是運行Listing E中代碼后的輸出:
Starting thread... Waiting for connection... Asking thread to stop... accept() failed or interrupted... Thread exiting under request... Stopping application...
多線程是一個強大的工具,然而它正呈現(xiàn)出一系列難題。其中之一是如何中斷一個正在運行的線程。如果恰當?shù)貙崿F(xiàn),使用上述技術(shù)中斷線程將比使用Java平臺上已經(jīng)提供的內(nèi)嵌操作更為簡單。
以上所述是小編給大家介紹的Java interrupt()方法使用注意,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Java?獲取Zookeeper節(jié)點下所有數(shù)據(jù)詳細步驟
本文介紹了如何使用Java獲取ZooKeeper節(jié)點下所有數(shù)據(jù),實際應(yīng)用示例中,我們演示了如何從ZooKeeper節(jié)點下獲取配置信息并輸出到控制臺,ZooKeeper是一個開源的分布式協(xié)調(diào)服務(wù),適用于分布式系統(tǒng)中的數(shù)據(jù)同步、配置管理、命名服務(wù)等功能,感興趣的朋友一起看看吧2024-11-11
servlet實現(xiàn)簡單的權(quán)限管理和敏感詞過濾功能
JavaEE課要求用servlet和過濾器實現(xiàn)權(quán)限管理和敏感詞過濾功能,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2021-05-05
Java操作redis實現(xiàn)增刪查改功能的方法示例
這篇文章主要介紹了Java操作redis實現(xiàn)增刪查改功能的方法,涉及java操作redis數(shù)據(jù)庫的連接、設(shè)置、增刪改查、釋放資源等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
詳解OpenFeign服務(wù)調(diào)用(微服務(wù))
OpenFeign是Spring Cloud在Feign的基礎(chǔ)上支持了SpringMVC的注解,如@RequesMapping等等,這篇文章主要介紹了OpenFeign服務(wù)調(diào)用的相關(guān)知識,需要的朋友可以參考下2022-07-07
使用SpringBoot 配置Oracle和H2雙數(shù)據(jù)源及問題
這篇文章主要介紹了使用SpringBoot 配置Oracle和H2雙數(shù)據(jù)源及問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

