當前位置:才華齋>計算機>java語言>

java中如何停止執行緒

java語言 閱讀(8.21K)

Java是一門面向物件程式語言,不僅吸收了C++語言的各種優點,還摒棄了C++裡難以理解的多繼承、指標等概念。想知道java中如何停止執行緒?下面就一起來了解看看吧!

java中如何停止執行緒

一般來說執行緒執行完run()之後就自動結束了,不過有些時候我們需要執行緒不停的做一些事情,也就是使用while迴圈,那麼這時候該如何停止執行緒呢?

這個問題需要分情況來討論,如果執行緒做的事情不是耗時的,那麼只需要使用一個標誌即可,具體的程式碼如下:

class MyThread extends Thread {

private volatile boolean isStop = false;

public void run() {

while (!isStop) {

tln("do something");

}

}

public void setStop() {

isStop = true;

}

}

如果需要退出時,呼叫setStop()即可。這裡使用了一個Java關鍵字volatile,這個關鍵字的目的是使isStop同步,也就是說在同一時刻只能由一個執行緒來修改isStop的值。

如果執行緒做的'事情是耗時或者說阻塞的(如呼叫了sleep,同步鎖的wait,socket的receiver,accept等方法),那麼就需要用到interrupt()了,呼叫該函式時會丟擲InterruptedException異常,程式碼中通過捕獲該異常,然後break出迴圈,就可以了。程式碼如下:

class MyThread extends Thread {

private volatile boolean isStop = false;

public void run() {

while (!isStop) {

try {

tln("do something");

p(1000);

} catch (InterruptedException e) {

tStackTrace();

break;//如果沒有這一句,而且不呼叫setStop(),執行緒並不會結束,需要特別注意一下

}

}

}

public void setStop() {

isStop = true;

}

}

public class Tes1 {

public static void main(String[] args) {

MyThread t1 = new MyThread();

t();

try {

p(10);

top();

rrupt();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

tStackTrace();

}

}

}

這裡既然說到了interrupt(),那麼就不得不提另外兩個函式:interrupted()和isInterrupted。看下這兩個函式的作用:

public static boolean interrupted()

測試當前執行緒是否已經中斷,並且清除執行緒的中斷狀態。

public boolean isInterrupted()

測試執行緒是否已經中斷,執行緒的中斷狀態不受該方法的影響。

這兩個函式的返回值含義:

如果當前執行緒已經中斷,則返回 true;否則返回 false。

其實當執行緒阻塞並且呼叫了interrupt()時,不止是丟擲InterruptedException異常還會呼叫interrupted()來清除執行緒的中斷狀態,所以在catch裡面呼叫isInterrupted()會返回false。

為了更好的理解這兩個函式,我們看個例子:

class MyThread extends Thread {

private int counter = 0;

public void run() {

boolean done = false;

try{

p(10);

}catch(InterruptedException ie){

tStackTrace();

return;

}

while (counter < 2000 &&!done) {

tln(counter++ + " in thread isInterrupted() "+isInterrupted());

if(isInterrupted()==true){

try{

tln("in thread after interrupted() "+isInterrupted());

sleep(100);

break;

}catch(InterruptedException ie){

tStackTrace();

break;

}

}

}

}

}

public class Tes1 {

public static void main(String[] args) {

final MyThread t1 = new MyThread();

t();

new Timer(true)dule(new TimerTask() {

public void run() {

tln("exec interrupt");

rrupt();

tln("in timer isInterrupted() "+terrupted());

}

}, 20);

}

}

執行效果如下(每一次執行列印的都不一樣,因為沒有做執行緒同步):

...

1300 in thread isInterrupted() false

exec interrupt

in timer isInterrupted() true

1301 in thread isInterrupted() true

in thread after interrupted() true

rruptedException: sleep interrupted

at p(Native Method)

at ()

還可能是這樣:

...

1568 in thread isInterrupted() false

exec interrupt

in thread after interrupted() true

in timer isInterrupted() true

rruptedException: sleep interrupted

at p(Native Method)

at ()

由於在sleep時執行了interrupt()所以會丟擲異常。

如果把if(isInterrupted()==true)改成if(rrupted()==true)那麼列印的結果就不同了,如下:

...

1582 in thread isInterrupted() false

exec interrupt

in timer isInterrupted() true

in thread after interrupted() false

還可能是這樣:

...

1771 in thread isInterrupted() false

exec interrupt

in timer isInterrupted() true

1772 in thread isInterrupted() false

in thread after interrupted() false

也可能是這樣:

...

476 in thread isInterrupted() false

in thread after interrupted() false

in timer isInterrupted() false

雖然執行了interrupt(),但是由於rrupted()可以清除中斷,所以並不會丟擲異常。

前面我們針對執行緒是否耗時,給出了停止執行緒的方法,其實還有一個辦法那就是使用()來強行終止執行緒,不過這個由於該方法不安全已經廢棄掉了,因為他有下面兩個缺陷:

1. 立即丟擲ThreadDeath異常,線上程的run()方法內,任何一點都有可能丟擲ThreadDeath異常,包括在catch或finally語句中。

2. 釋放該執行緒所持有的所有的鎖。