java isInterrupted()判斷線程的實(shí)例講解
isInterrupted()可以判斷當(dāng)前線程是否被中斷,僅僅是對interrupt()標(biāo)識的一個(gè)判斷,并不會(huì)影響標(biāo)識發(fā)生任何改變(因?yàn)檎{(diào)用interrupt()的時(shí)候會(huì)設(shè)置內(nèi)部的一個(gè)叫interrupt flag的標(biāo)識)。
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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 利用CSS3新特性創(chuàng)建透明邊框三角2. AJAX實(shí)現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺】3. Python collections模塊的使用方法4. Python腳本文件外部傳遞參數(shù)的處理方法5. ajax post下載flask文件流以及中文文件名問題6. Python調(diào)用ffmpeg開源視頻處理庫,批量處理視頻7. 使用css實(shí)現(xiàn)全兼容tooltip提示框8. WML語言的基本情況9. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能10. PHP 使用 Trait 解決 PHP 單繼承問題詳解
