日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java中u怎么用_Java中interrupt的使用

發布時間:2023/12/1 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中u怎么用_Java中interrupt的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

通常我們會有這樣的需求,即停止一個線程。在java的api中有stop、suspend等方法可以達到目的,但由于這些方法在使用上存在不安全性,會帶來不好的副作用,不建議被使用。具體原因可以參考Why is?Thread.stop?deprecated。

在本文中,將討論中斷在java中的使用。

中斷在java中主要有3個方法,interrupt(),isInterrupted()和interrupted()。

interrupt(),在一個線程中調用另一個線程的interrupt()方法,即會向那個線程發出信號——線程中斷狀態已被設置。至于那個線程何去何從,由具體的代碼實現決定。

isInterrupted(),用來判斷當前線程的中斷狀態(true or false)。

interrupted()是個Thread的static方法,用來恢復中斷狀態,名字起得額🙄。

接下來,看看具體在代碼中如何使用。

interrupt()不能中斷在運行中的線程,它只能改變中斷狀態而已。

public class InterruptionInJava implementsRunnable{public static void main(String[] args) throwsInterruptedException {

Thread testThread= new Thread(new InterruptionInJava(),"InterruptionInJava");//start thread

testThread.start();

Thread.sleep(1000);//interrupt thread

testThread.interrupt();

System.out.println("main end");

}

@Overridepublic voidrun() {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

那么,如何正確中斷?

既然是只能修改中斷狀態,那么我們應該針對中斷狀態做些什么。

public class InterruptionInJava implementsRunnable{public static void main(String[] args) throwsInterruptedException {

Thread testThread= new Thread(new InterruptionInJava(),"InterruptionInJava");//start thread

testThread.start();//Thread.sleep(1000);//interrupt thread

testThread.interrupt();

System.out.println("main end");

}

@Overridepublic voidrun() {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");

}

}

}

}

修改代碼,在狀態判斷中如上,添加一個return就okay了。但現實中,我們可能需要做的更通用,不禁又要發出天問,如何中斷線程?答案是添加一個開關。

public class InterruptionInJava implementsRunnable{private volatile static boolean on = false;public static void main(String[] args) throwsInterruptedException {

Thread testThread= new Thread(new InterruptionInJava(),"InterruptionInJava");//start thread

testThread.start();

Thread.sleep(1000);

InterruptionInJava.on= true;

System.out.println("main end");

}

@Overridepublic voidrun() {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 implementsRunnable{private volatile static boolean on = false;public static void main(String[] args) throwsInterruptedException {

Thread testThread= new Thread(new InterruptionInJava(),"InterruptionInJava");//start thread

testThread.start();

Thread.sleep(1000);

InterruptionInJava.on= true;

System.out.println("main end");

}

@Overridepublic voidrun() {while(!on){try{

Thread.sleep(10000000);

}catch(InterruptedException e) {

System.out.println("caught exception: "+e);

}

}

}

}

線程被阻塞無法被中斷。這時候救世主interrupt函數又回來了,它可以迅速中斷被阻塞的線程,拋出一個InterruptedException,把線程從阻塞狀態中解救出來,show the code。

public class InterruptionInJava implementsRunnable{private volatile static boolean on = false;public static void main(String[] args) throwsInterruptedException {

Thread testThread= new Thread(new InterruptionInJava(),"InterruptionInJava");//start thread

testThread.start();

Thread.sleep(1000);

InterruptionInJava.on= true;

testThread.interrupt();

System.out.println("main end");

}

@Overridepublic voidrun() {while(!on){try{

Thread.sleep(10000000);

}catch(InterruptedException e) {

System.out.println("caught exception right now: "+e);

}

}

}

這種情形同樣適用io阻塞,通常io阻塞會立即拋出一個SocketException,類似于上面說的InterruptedException。

總結

以上是生活随笔為你收集整理的java中u怎么用_Java中interrupt的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。