java isInterrupted()判斷線程的實例講解
1、說明
isInterrupted()可以判斷當前線程是否被中斷,僅僅是對interrupt()標識的一個判斷,并不會影響標識發(fā)生任何改變(因為調用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()如何判斷線程內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決IDEA報錯Failed?to?start?bean‘documentationPluginsBootstra
這篇文章主要介紹了解決IDEA報錯Failed?to?start?bean‘documentationPluginsBootstrapper‘問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07JAVA中ArrayList和數(shù)組的轉換與遇到的問題解決
做研發(fā)的朋友都知道,在項目開發(fā)中經常會碰到ArrayList與數(shù)組類型之間的相互轉換,這篇文章主要給大家介紹了關于JAVA中ArrayList和數(shù)組的轉換與遇到的問題解決,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-05-05詳解springboot整合ehcache實現(xiàn)緩存機制
這篇文章主要介紹了詳解springboot整合ehcache實現(xiàn)緩存機制,ehcache提供了多種緩存策略,主要分為內存和磁盤兩級,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01maven坐標Dependencies和Exclusions的使用
這篇文章主要介紹了maven坐標Dependencies和Exclusions的使用,很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12