Java多線(xiàn)程 中斷機(jī)制及實(shí)例詳解
正文
這里詳細(xì)分析interrupt(),interrupted(),isInterrupted()三個(gè)方法
interrupt()
中斷這個(gè)線(xiàn)程,設(shè)置中斷標(biāo)識(shí)位
public void interrupt() { if (this != Thread.currentThread()) checkAccess(); synchronized (blockerLock) { Interruptible b = blocker; if (b != null) { interrupt0(); // Just to set the interrupt flag b.interrupt(this); return; } } interrupt0(); }
我們來(lái)找下如何設(shè)置中斷標(biāo)識(shí)位的
找到interrupt0()的源碼,src/hotspot/share/prims/jvm.cpp
JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread)) ... if (is_alive) { // jthread refers to a live JavaThread. Thread::interrupt(receiver); } JVM_END
調(diào)用了Thread::interrupt方法
src/hotspot/share/runtime/thread.cpp
void Thread::interrupt(Thread* thread) { ... os::interrupt(thread); }
os::interrupt方法,src/hotspot/os/posix/os_posix.cpp
void os::interrupt(Thread* thread) { ... OSThread* osthread = thread->osthread(); if (!osthread->interrupted()) { //設(shè)置中斷標(biāo)識(shí)位 osthread->set_interrupted(true); ... } ... }
isInterrupted()
測(cè)試線(xiàn)程是否被中斷,線(xiàn)程的中斷狀態(tài)不會(huì)改變
public boolean isInterrupted() { return isInterrupted(false); }
查看native isInterrupted(boolean ClearInterrupted)源碼,查找方式同上
src/hotspot/os/posix/os_posix.cpp
bool os::is_interrupted(Thread* thread, bool clear_interrupted) { debug_only(Thread::check_for_dangling_thread_pointer(thread);) OSThread* osthread = thread->osthread(); // 查看是否被中斷 bool interrupted = osthread->interrupted(); // 清除標(biāo)識(shí)位后再設(shè)置false if (interrupted && clear_interrupted) { osthread->set_interrupted(false); } return interrupted; }
Java傳遞ClearInterrupted為false,對(duì)應(yīng)C++的clear_interrupted
interrupted()
測(cè)試線(xiàn)程是否被中斷,清除中斷標(biāo)識(shí)位
public static boolean interrupted() { return currentThread().isInterrupted(true); }
簡(jiǎn)單的例子
public class MyThread45 { public static void main(String[] args) throws Exception { Runnable runnable = new Runnable() { public void run() { while (true) { if (Thread.currentThread().isInterrupted()) { System.out.println("線(xiàn)程被中斷了"); return ; } else { System.out.println("線(xiàn)程沒(méi)有被中斷"); } } } }; Thread t = new Thread(runnable); t.start(); Thread.sleep(500); t.interrupt(); System.out.println("線(xiàn)程中斷了,程序到這里了"); } }
檢查線(xiàn)程是否中斷,中斷線(xiàn)程,運(yùn)行結(jié)果如下
······ 線(xiàn)程沒(méi)有被中斷 線(xiàn)程沒(méi)有被中斷 線(xiàn)程沒(méi)有被中斷 線(xiàn)程被中斷了 線(xiàn)程中斷了,程序到這里了
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java http token請(qǐng)求代碼實(shí)例
這篇文章主要介紹了java http token請(qǐng)求,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03關(guān)于Spring自定義XML schema 擴(kuò)展的問(wèn)題(Spring面試高頻題)
今天給大家分享一道spring高頻率面試題關(guān)于Spring自定義XML schema 擴(kuò)展的問(wèn)題,今天以spring整合dubbo的實(shí)例給大家詳細(xì)講解下,感興趣的朋友跟隨小編一起看看吧2021-05-05Java日期工具類(lèi)時(shí)間校驗(yàn)實(shí)現(xiàn)
一般項(xiàng)目中需要對(duì)入?yún)⑦M(jìn)行校驗(yàn),比如必須是一個(gè)合法的日期,本文就來(lái)介紹一下Java日期工具類(lèi)時(shí)間校驗(yàn)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12SpringBoot實(shí)現(xiàn)excel文件生成和下載
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)excel文件生成和下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02springBoot 與neo4j的簡(jiǎn)單整合示例
這篇文章主要介紹了springBoot 與neo4j的簡(jiǎn)單整合示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01java synchronized同步靜態(tài)方法和同步非靜態(tài)方法的異同
這篇文章主要介紹了java synchronized同步靜態(tài)方法和同步非靜態(tài)方法的異同的相關(guān)資料,需要的朋友可以參考下2017-01-01SpringBoot多線(xiàn)程與任務(wù)調(diào)度總結(jié)
多線(xiàn)程與任務(wù)調(diào)度是java開(kāi)發(fā)中必須掌握的技能,本文主要介紹了SpringBoot多線(xiàn)程與任務(wù)調(diào)度總結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12SpringBoot 2.0 整合sharding-jdbc中間件實(shí)現(xiàn)數(shù)據(jù)分庫(kù)分表
這篇文章主要介紹了SpringBoot 2.0 整合sharding-jdbc中間件,實(shí)現(xiàn)數(shù)據(jù)分庫(kù)分表,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-06-06Java線(xiàn)程安全和鎖Synchronized知識(shí)點(diǎn)詳解
在本篇文章里小編給大家分享的是關(guān)于Java線(xiàn)程安全和鎖Synchronized相關(guān)知識(shí)點(diǎn),有需要的朋友們可以參考下。2019-08-08