java isInterrupted()判斷線程的實(shí)例講解
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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 編程語言PHP在Web開發(fā)領(lǐng)域的優(yōu)勢(shì)在哪?2. 詳解Android studio 動(dòng)態(tài)fragment的用法3. 圖文詳解vue中proto文件的函數(shù)調(diào)用4. 什么是python的自省5. 基于android studio的layout的xml文件的創(chuàng)建方式6. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁效果(實(shí)例代碼)7. 解決Android studio xml界面無法預(yù)覽問題8. Android如何加載Base64編碼格式圖片9. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)10. Springboot Druid 自定義加密數(shù)據(jù)庫密碼的幾種方案

網(wǎng)公網(wǎng)安備