java isInterrupted()判斷線程的實例講解
isInterrupted()可以判斷當前線程是否被中斷,僅僅是對interrupt()標識的一個判斷,并不會影響標識發生任何改變(因為調用interrupt()的時候會設置內部的一個叫interrupt flag的標識)。
2、實例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());}
實例擴展補充:
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);} }}
到此這篇關于java isInterrupted()判斷線程的實例講解的文章就介紹到這了,更多相關java isInterrupted()如何判斷線程內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: