最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java線程中的interrupt方法解讀

 更新時間:2023年10月17日 08:40:35   作者:huskyui  
這篇文章主要介紹了Java線程中的interrupt方法解讀,Java中的interrupt是一種線程間通信的機制,用于請求中斷線程的執(zhí)行。當一個線程調用另一個線程的interrupt()方法時,被調用線程會收到一個中斷信號,可以根據需要做出相應的處理,需要的朋友可以參考下

interrupt方法

首先梳理Thread關于interrupt的定義

/**
 * Interrupts this thread.
 *
 * <p> Unless the current thread is interrupting itself, which is
 * always permitted, the {@link #checkAccess() checkAccess} method
 * of this thread is invoked, which may cause a {@link
 * SecurityException} to be thrown.
 *
 * <p> If this thread is blocked in an invocation of the {@link
 * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
 * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
 * class, or of the {@link #join()}, {@link #join(long)}, {@link
 * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
 * methods of this class, then its interrupt status will be cleared and it
 * will receive an {@link InterruptedException}.
 *
 * <p> If this thread is blocked in an I/O operation upon an {@link
 * java.nio.channels.InterruptibleChannel InterruptibleChannel}
 * then the channel will be closed, the thread's interrupt
 * status will be set, and the thread will receive a {@link
 * java.nio.channels.ClosedByInterruptException}.
 *
 * <p> If this thread is blocked in a {@link java.nio.channels.Selector}
 * then the thread's interrupt status will be set and it will return
 * immediately from the selection operation, possibly with a non-zero
 * value, just as if the selector's {@link
 * java.nio.channels.Selector#wakeup wakeup} method were invoked.
 *
 * <p> If none of the previous conditions hold then this thread's interrupt
 * status will be set. </p>
 *
 * <p> Interrupting a thread that is not alive need not have any effect.
 *
 * @throws  SecurityException
 *          if the current thread cannot modify this thread
 *
 * @revised 6.0
 * @spec JSR-51
 */
public void interrupt() 

這里講述了Thread實例對象調用方法interrupt,有這幾種情況

? 1.在線程里面有wait,sleep,join方法,會彈出InterruptedException,狀態(tài)會被清除

? 2.io里面,會彈出異常,狀態(tài)會被設置,…

? 3.如果線程阻塞在nio中的Selector中,狀態(tài)會被設置,…

? 4.如果不是上面,這些情況,狀態(tài)會被設置

可以看出interrupt是關于狀態(tài)的設置

我們來驗證一下

第一種,沒有任何情況的

public static void main(String[] args) {
    Thread t1 = new Thread(()->{
       while(true){
           if (interrupted()) {
               break;
           }
           System.out.println("i" + new Date());
       }
    });
    t1.start();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    t1.interrupt();
}

線程會在(1000+)ms后結束,整個程序也會結束

第二種,有sleep的情況下

public static void main(String[] args) {
    Thread t1 = new Thread(()->{
       while(true){
           if (interrupted()) {
               break;
           }
           try {
               Thread.sleep(100);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }

           System.out.println("i" + new Date());
       }
    });
    t1.start();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    t1.interrupt();
}

方法并不會退出,只是打出了一個InterruptException

如何處理中斷?

  • 上文都在介紹如何獲取中斷狀態(tài),那么當我們捕獲到中斷狀態(tài)后,究竟如何處理呢?
  • Java類庫中提供的一些可能會發(fā)生阻塞的方法都會拋InterruptedException異常,如:BlockingQueue#put、BlockingQueue#take、Object#wait、Thread#sleep。
  • 當你在某一條線程中調用這些方法時,這個方法可能會被阻塞很長時間,你可以在別的線程中調用當前線程對象的interrupt方法觸發(fā)這些函數(shù)拋出InterruptedException異常。
  • 當一個函數(shù)拋出InterruptedException異常時,表示這個方法阻塞的時間太久了,別人不想等它執(zhí)行結束了。
  • 當你的捕獲到一個InterruptedException異常后,亦可以處理它,或者向上拋出。
  • 拋出時要注意???:當你捕獲到InterruptedException異常后,當前線程的中斷狀態(tài)已經被修改為false(表示線程未被中斷);此時你若能夠處理中斷,則不用理會該值;但如果你繼續(xù)向上拋InterruptedException異常,你需要再次調用interrupt方法,將當前線程的中斷狀態(tài)設為true。
  • 注意:絕對不能“吞掉中斷”!即捕獲了InterruptedException而不作任何處理。這樣違背了中斷機制的規(guī)則,別人想讓你線程中斷,然而你自己不處理,也不將中斷請求告訴調用者,調用者一直以為沒有中斷請求。

更新相關interrupt方法的意義

public void interrupt()

Thread里面的打斷函數(shù),可以使用新建一個Thread對象,執(zhí)行thread.interrupt();

修改調用線程的interrupt 的status

public static boolean interrupted()

該方法可以判斷是否被打斷,但是這個interrupt的status會被清除,

/**
     * Tests whether the current thread has been interrupted.  The
     * <i>interrupted status</i> of the thread is cleared by this method.  In
     * other words, if this method were to be called twice in succession, the
     * second call would return false (unless the current thread were
     * interrupted again, after the first call had cleared its interrupted
     * status and before the second call had examined it).
     *
     * <p>A thread interruption ignored because a thread was not alive
     * at the time of the interrupt will be reflected by this method
     * returning false.
     *
     * @return  <code>true</code> if the current thread has been interrupted;
     *          <code>false</code> otherwise.
     * @see #isInterrupted()
     * @revised 6.0
     */
public boolean isInterrupted()

該方法可以判斷是否被打斷

到此這篇關于Java線程中的interrupt方法解讀的文章就介紹到這了,更多相關interrupt方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • SpringBoot聲明式事務的簡單運用說明

    SpringBoot聲明式事務的簡單運用說明

    這篇文章主要介紹了SpringBoot聲明式事務的簡單運用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • java文字轉語音播報功能的實現(xiàn)方法

    java文字轉語音播報功能的實現(xiàn)方法

    這篇文章主要給大家介紹了關于java文字轉語音播報功能的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用java具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07
  • @Scheduled在springboot中的使用方式

    @Scheduled在springboot中的使用方式

    這篇文章主要介紹了@Scheduled在springboot中的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • java8?Stream大數(shù)據量List分批處理切割方式

    java8?Stream大數(shù)據量List分批處理切割方式

    這篇文章主要介紹了java8?Stream大數(shù)據量List分批處理切割方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Java語言實現(xiàn)簡單FTP軟件 輔助功能模塊FTP站點管理實現(xiàn)(12)

    Java語言實現(xiàn)簡單FTP軟件 輔助功能模塊FTP站點管理實現(xiàn)(12)

    這篇文章主要為大家詳細介紹了Java語言實現(xiàn)簡單FTP軟,輔助功能模塊FTP站點管理的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • SSM spring Bean基礎配置實例詳解

    SSM spring Bean基礎配置實例詳解

    Bean是Spring框架中的一個基本單元,通常是一個普通的Java對象(POJO),但它被Spring容器管理,本文給大家介紹SSM spring Bean基礎配置,感興趣的朋友一起看看吧
    2025-06-06
  • Vue實現(xiàn)驗證碼登錄的超詳細步驟

    Vue實現(xiàn)驗證碼登錄的超詳細步驟

    這篇文章主要給大家介紹了關于Vue實現(xiàn)驗證碼登錄的超詳細步驟,我們在使用vue進行前端開發(fā)時都需要登錄驗證,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2023-09-09
  • 使用Spring Boot上傳文件功能

    使用Spring Boot上傳文件功能

    上傳文件是互聯(lián)網中常應用的場景之一,最典型的情況就是上傳頭像等,今天就帶著大家做一個Spring Boot上傳文件的小案例,感興趣的朋友跟隨腳本之家小編一起學習吧
    2018-01-01
  • 擲6面骰子6000次每個點數(shù)出現(xiàn)的概率

    擲6面骰子6000次每個點數(shù)出現(xiàn)的概率

    今天小編就為大家分享一篇關于擲6面骰子6000次每個點數(shù)出現(xiàn)的概率,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • SpringBoot實戰(zhàn)之高效使用枚舉參數(shù)(原理篇)案例詳解

    SpringBoot實戰(zhàn)之高效使用枚舉參數(shù)(原理篇)案例詳解

    這篇文章主要介紹了SpringBoot實戰(zhàn)之高效使用枚舉參數(shù)(原理篇)案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-09-09

最新評論

章丘市| 澄城县| 中牟县| 富民县| 镇巴县| 陕西省| 北安市| 平武县| 梁河县| 曲周县| 石狮市| 鄂托克前旗| 大港区| 潼南县| 潜山县| 浪卡子县| 汝城县| 安图县| 乐清市| 灵寿县| 平安县| 新河县| 榆树市| 潼关县| 金溪县| 洛浦县| 云和县| 陈巴尔虎旗| 平昌县| 赣州市| 迁安市| 康定县| 江华| 大埔区| 铅山县| 稻城县| 苏尼特右旗| 富阳市| 定陶县| 兴和县| 平度市|