- 相關(guān)推薦
java中如何停止線(xiàn)程
Java是一門(mén)面向對象編程語(yǔ)言,不僅吸收了C++語(yǔ)言的各種優(yōu)點(diǎn),還摒棄了C++里難以理解的多繼承、指針等概念。想知道java中如何停止線(xiàn)程?下面就一起來(lái)了解看看吧!
一般來(lái)說(shuō)線(xiàn)程執行完run()之后就自動(dòng)結束了,不過(guò)有些時(shí)候我們需要線(xiàn)程不停的做一些事情,也就是使用while循環(huán),那么這時(shí)候該如何停止線(xiàn)程呢?
這個(gè)問(wèn)題需要分情況來(lái)討論,如果線(xiàn)程做的事情不是耗時(shí)的,那么只需要使用一個(gè)標志即可,具體的代碼如下:
class MyThread extends Thread {
private volatile boolean isStop = false;
public void run() {
while (!isStop) {
System.out.println("do something");
}
}
public void setStop() {
isStop = true;
}
}
如果需要退出時(shí),調用setStop()即可。這里使用了一個(gè)Java關(guān)鍵字volatile,這個(gè)關(guān)鍵字的目的是使isStop同步,也就是說(shuō)在同一時(shí)刻只能由一個(gè)線(xiàn)程來(lái)修改isStop的值。
如果線(xiàn)程做的事情是耗時(shí)或者說(shuō)阻塞的(如調用了sleep,同步鎖的wait,socket的receiver,accept等方法),那么就需要用到interrupt()了,調用該函數時(shí)會(huì )拋出InterruptedException異常,代碼中通過(guò)捕獲該異常,然后break出循環(huán),就可以了。代碼如下:
class MyThread extends Thread {
private volatile boolean isStop = false;
public void run() {
while (!isStop) {
try {
System.out.println("do something");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
break;//如果沒(méi)有這一句,而且不調用setStop(),線(xiàn)程并不會(huì )結束,需要特別注意一下
}
}
}
public void setStop() {
isStop = true;
}
}
public class Tes1 {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();
try {
Thread.sleep(10);
t1.setStop();
t1.interrupt();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
這里既然說(shuō)到了interrupt(),那么就不得不提另外兩個(gè)函數:interrupted()和isInterrupted?聪逻@兩個(gè)函數的作用:
public static boolean interrupted()
測試當前線(xiàn)程是否已經(jīng)中斷,并且清除線(xiàn)程的中斷狀態(tài)。
public boolean isInterrupted()
測試線(xiàn)程是否已經(jīng)中斷,線(xiàn)程的中斷狀態(tài)不受該方法的影響。
這兩個(gè)函數的返回值含義:
如果當前線(xiàn)程已經(jīng)中斷,則返回 true;否則返回 false。
其實(shí)當線(xiàn)程阻塞并且調用了interrupt()時(shí),不止是拋出InterruptedException異常還會(huì )調用interrupted()來(lái)清除線(xiàn)程的中斷狀態(tài),所以在catch里面調用isInterrupted()會(huì )返回false。
為了更好的理解這兩個(gè)函數,我們看個(gè)例子:
class MyThread extends Thread {
private int counter = 0;
public void run() {
boolean done = false;
try{
Thread.sleep(10);
}catch(InterruptedException ie){
ie.printStackTrace();
return;
}
while (counter < 2000 &&!done) {
System.out.println(counter++ + " in thread isInterrupted() "+isInterrupted());
if(isInterrupted()==true){
try{
System.out.println("in thread after interrupted() "+isInterrupted());
sleep(100);
break;
}catch(InterruptedException ie){
ie.printStackTrace();
break;
}
}
}
}
}
public class Tes1 {
public static void main(String[] args) {
final MyThread t1 = new MyThread();
t1.start();
new Timer(true).schedule(new TimerTask() {
public void run() {
System.out.println("exec interrupt");
t1.interrupt();
System.out.println("in timer isInterrupted() "+t1.isInterrupted());
}
}, 20);
}
}
執行效果如下(每一次執行打印的都不一樣,因為沒(méi)有做線(xiàn)程同步):
...
1300 in thread isInterrupted() false
exec interrupt
in timer isInterrupted() true
1301 in thread isInterrupted() true
in thread after interrupted() true
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.example.wd.MyThread.run(Tes1.java:21)
還可能是這樣:
...
1568 in thread isInterrupted() false
exec interrupt
in thread after interrupted() true
in timer isInterrupted() true
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.example.wd.MyThread.run(Tes1.java:21)
由于在sleep時(shí)執行了interrupt()所以會(huì )拋出異常。
如果把if(isInterrupted()==true)改成if(Thread.interrupted()==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(),但是由于Thread.interrupted()可以清除中斷,所以并不會(huì )拋出異常。
前面我們針對線(xiàn)程是否耗時(shí),給出了停止線(xiàn)程的方法,其實(shí)還有一個(gè)辦法那就是使用thread.stop()來(lái)強行終止線(xiàn)程,不過(guò)這個(gè)由于該方法不安全已經(jīng)廢棄掉了,因為他有下面兩個(gè)缺陷:
1. 立即拋出ThreadDeath異常,在線(xiàn)程的run()方法內,任何一點(diǎn)都有可能拋出ThreadDeath異常,包括在catch或finally語(yǔ)句中。
2. 釋放該線(xiàn)程所持有的所有的鎖。
【java中如何停止線(xiàn)程】相關(guān)文章:
如何使用java多線(xiàn)程05-04
如何創(chuàng )建并運行Java線(xiàn)程01-11
JAVA中終止線(xiàn)程的方法08-12
Java線(xiàn)程編程中的主線(xiàn)程詳細介紹01-31
java Runnable接口如何創(chuàng )建線(xiàn)程05-18
淺談如何使用java多線(xiàn)程05-07
java的多線(xiàn)程04-09