java isInterrupted()判斷線程的實(shí)例講解
isInterrupted()可以判斷當(dāng)前線程是否被中斷,僅僅是對interrupt()標(biāo)識的一個判斷,并不會影響標(biāo)識發(fā)生任何改變(因?yàn)檎{(diào)用interrupt()的時候會設(shè)置內(nèi)部的一個叫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)文章:
