java中如何停止線程
Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++裏難以理解的多繼承、指針等概念。想知道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. 釋放該線程所持有的所有的鎖。