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)文章
解決IDEA報(bào)錯(cuò)Failed?to?start?bean‘documentationPluginsBootstra
這篇文章主要介紹了解決IDEA報(bào)錯(cuò)Failed?to?start?bean‘documentationPluginsBootstrapper‘問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
JAVA中ArrayList和數(shù)組的轉(zhuǎn)換與遇到的問題解決
做研發(fā)的朋友都知道,在項(xiàng)目開發(fā)中經(jīng)常會(huì)碰到ArrayList與數(shù)組類型之間的相互轉(zhuǎn)換,這篇文章主要給大家介紹了關(guān)于JAVA中ArrayList和數(shù)組的轉(zhuǎn)換與遇到的問題解決,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
詳解springboot整合ehcache實(shí)現(xiàn)緩存機(jī)制
這篇文章主要介紹了詳解springboot整合ehcache實(shí)現(xiàn)緩存機(jī)制,ehcache提供了多種緩存策略,主要分為內(nèi)存和磁盤兩級(jí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Java 字符串轉(zhuǎn)float運(yùn)算 float轉(zhuǎn)字符串的方法
今天小編就為大家分享一篇Java 字符串轉(zhuǎn)float運(yùn)算 float轉(zhuǎn)字符串的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Java判斷List中相同值元素的個(gè)數(shù)實(shí)例
今天小編就為大家分享一篇Java判斷List中相同值元素的個(gè)數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
maven坐標(biāo)Dependencies和Exclusions的使用
這篇文章主要介紹了maven坐標(biāo)Dependencies和Exclusions的使用,很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

