Java中的interrupt、interrupted和isInterrupted方法區(qū)別詳解
一、interrupt
interrupt用于中斷線程。調(diào)用該方法的線程的狀態(tài)將會被設(shè)置為“中斷狀態(tài)”。 【注意】線程中斷僅僅是設(shè)置線程的中斷狀態(tài)位,并不會停止線程。需要用戶自己去監(jiān)視線程的狀態(tài)并作出處理
1、打斷正常運行的線程
public class InterruptTest { public static void main(String[] args) { Thread t1 = new Thread(new Runnable() { @Override public void run() { int i = 0; while (true) { System.out.println(i++); } } }); t1.start(); System.out.println("主線程正常運行中....."); System.out.println("打斷t1線程"); t1.interrupt(); System.out.println("t1線程的打斷標記:" + t1.isInterrupted()); System.exit(0); } }
【結(jié)論】t1線程被打斷后,t1線程并沒有中止,而是繼續(xù)運行,只是將打斷標記設(shè)置為true
2、使用interrupt打斷sleep中的線程
使用interrupt打斷sleep中的線程,會拋出異常,并且清除打斷標記
private static void test1() throws InterruptedException { Thread t1 = new Thread(()->{ sleep(1); }, "t1"); t1.start(); sleep(0.5); t1.interrupt(); log.debug(" 打斷狀態(tài): {}", t1.isInterrupted()); }
3、使用interrupt打斷join中的線程
使用interrupt打斷join中的線程,會拋出異常,但是打斷標記還是會置為true
private static void test11() throws InterruptedException { Thread t1 = new Thread(()->{ int i = 0; while (i < 9999999) { i++; } }, "t1"); Thread t2 = new Thread(()->{ try { t1.join(); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println("等待t1線程執(zhí)行結(jié)束"); }, "t2"); t1.start(); t2.start(); t2.interrupt(); log.debug("t2打斷狀態(tài): {}", t2.isInterrupted()); }
4、打斷 park 線程
之后補充,先了解下 park() 方法的使用
二、isInterrupted
查詢當前線程的中斷標志位是true還是false,利用中斷標志位來結(jié)束線程
public class InterruptTest { public static void main(String[] args) { Thread t1 = new Thread(new Runnable() { @Override public void run() { int i = 0; // 當t1線程被打斷后,條件不滿足,t1線程執(zhí)行結(jié)束 while (!Thread.currentThread().isInterrupted()) { System.out.println(i++); } } }); t1.start(); try { Thread.sleep(2000); } catch (InterruptedException e) { throw new RuntimeException(e); } t1.interrupt(); } }
三、interrupted
interrupted是靜態(tài)方法,查看線程中斷信號是true還是false,并且清除中斷信號;如果一個線程被中斷了,第一次調(diào)用interrupted返回的是true,第二次調(diào)用則返回的是false
public class InterrupttedTest { public static void main(String[] args) { Thread t1 = new Thread(new Runnable() { @Override public void run() { int i = 0; while (true) { System.out.println(i++); System.out.println("查看線程的中斷標志位:"+ Thread.interrupted()); } } }); t1.start(); t1.interrupt(); try { Thread.sleep(500); } catch (InterruptedException e) { throw new RuntimeException(e); } t1.stop(); } }
到此這篇關(guān)于Java中的interrupt、interrupted和isInterrupted方法區(qū)別詳解的文章就介紹到這了,更多相關(guān)interrupt、interrupted和isInterrupted方法區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java反射獲取所有Controller和RestController類的方法
這篇文章給大家分享了Java反射獲取所有Controller和RestController類的方法,文中有詳細的代碼示例講解,具有一定的參考價值,需要的朋友可以參考下2023-08-08java中利用List的subList方法實現(xiàn)對List分頁(簡單易學)
本篇文章主要介紹了java中l(wèi)ist數(shù)據(jù)拆分為sublist實現(xiàn)頁面分頁的簡單代碼,具有一定的參考價值,有需要的可以了解一下。2016-11-11SpringBoot項目離線環(huán)境手動構(gòu)建的過程
文章介紹了如何在IntelliJ IDEA中手動創(chuàng)建一個Spring Boot項目,并詳細講解了pom.xml文件的配置和基本項目結(jié)構(gòu)的設(shè)置,感興趣的朋友跟隨小編一起看看吧2025-01-01詳解Java中的checked異常和unchecked異常區(qū)別
這篇文章主要介紹了詳解Java中的checked異常和unchecked異常區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02