java isInterrupted()判斷線程的實(shí)例講解
1、說(shuō)明
isInterrupted()可以判斷當(dāng)前線程是否被中斷,僅僅是對(duì)interrupt()標(biāo)識(shí)的一個(gè)判斷,并不會(huì)影響標(biāo)識(shí)發(fā)生任何改變(因?yàn)檎{(diào)用interrupt()的時(shí)候會(huì)設(shè)置內(nèi)部的一個(gè)叫interrupt flag的標(biāo)識(shí))。
2、實(shí)例
public static void main(String[] args) throws InterruptedException{
Thread thread = new Thread(()->{
while (true){}
});
thread.start();
TimeUnit.SECONDS.sleep(1);
System.out.println("Thread is interrupted :"+thread.isInterrupted());
thread.interrupt();
System.out.println("Thread is interrupted :"+thread.isInterrupted());
}
實(shí)例擴(kuò)展補(bǔ)充:
ublic class t12 {
public static void main(String[] args) {
try {
MyThread12 thread = new MyThread12();
thread.start();
Thread.sleep(500);
thread.interrupt();
System.out.println("是否終止1? =" + thread.interrupted());
System.out.println("是否終止2? =" + thread.interrupted());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("-------------end-------------");
}
}
class MyThread12 extends Thread {
public void run() {
for (int i = 0; i < 50000; i++) {
System.out.println("i = " + i);
}
}
}
到此這篇關(guān)于java isInterrupted()判斷線程的實(shí)例講解的文章就介紹到這了,更多相關(guān)java isInterrupted()如何判斷線程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Spring中@Transactional事務(wù)回滾的注意事項(xiàng)
這篇文章主要介紹了關(guān)于Spring中@Transactional事務(wù)回滾的注意事項(xiàng),回滾(Rollback)指的是程序或數(shù)據(jù)處理錯(cuò)誤,將程序或數(shù)據(jù)恢復(fù)到上一次正確狀態(tài)的行為?;貪L包括程序回滾和數(shù)據(jù)回滾等類型,需要的朋友可以參考下2023-05-05
Spring?JPA的實(shí)體屬性類型轉(zhuǎn)換器并反序列化工具類詳解
這篇文章主要介紹了Spring?JPA的實(shí)體屬性類型轉(zhuǎn)換器并反序列化工具類詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
淺談mybatis中的#和$的區(qū)別 以及防止sql注入的方法
下面小編就為大家?guī)?lái)一篇淺談mybatis中的#和$的區(qū)別 以及防止sql注入的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
mybatis plus or and 的合并寫(xiě)法實(shí)例
這篇文章主要介紹了mybatis plus or and 的合并寫(xiě)法實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
Spring Boot項(xiàng)目維護(hù)全局json數(shù)據(jù)代碼實(shí)例
這篇文章主要介紹了Spring Boot項(xiàng)目維護(hù)全局json數(shù)據(jù)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
解決@Transaction注解導(dǎo)致動(dòng)態(tài)切換更改數(shù)據(jù)庫(kù)失效問(wèn)題
這篇文章主要介紹了解決@Transaction注解導(dǎo)致動(dòng)態(tài)切換更改數(shù)據(jù)庫(kù)失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09

