1. 程式人生 > 其它 >【jvm】中斷深入理解,執行緒池,AQS都在使用的利器

【jvm】中斷深入理解,執行緒池,AQS都在使用的利器

技術標籤:架構jvm面試

1 執行緒中斷:

java中對於中斷的大部分操作無外乎以下兩點:

  • 設定或者清除中斷標誌位(對執行狀態執行緒需要自檢,堵塞狀態會丟擲異常)
  • 丟擲InterruptedException

2 設定中斷

直接看原始碼 :對上面的兩個操作說的很清晰了

    /**
     * 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.
     * // 無前置條件,只是設定了中斷狀態欄位,執行緒需要自檢使用。入過設定中斷後,再進入堵塞,OS不會發送中斷訊號
     * <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() {
        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();
    }

3 自檢

isInterrupted() 和 interrupted() 方法只涉及到中斷狀態的查詢,最多是多加一步重置中斷狀態,並不牽涉到InterruptedException。
interrupted()是我們清除中斷的唯一方法

看原始碼:

  /**
     * 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 static boolean interrupted() {
        return currentThread().isInterrupted(true);  // true clean signal(cleared its interrupted)
    }

 /**
     * Tests whether this thread has been interrupted.  The <i>interrupted
     * status</i> of the thread is unaffected by this method.
     *
     * <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 this thread has been interrupted;
     *          <code>false</code> otherwise.
     * @see     #interrupted()
     * @revised 6.0
     */
    public boolean isInterrupted() {
        return isInterrupted(false);
    }

    /**
     * Tests if some Thread has been interrupted.  The interrupted state
     * is reset or not based on the value of ClearInterrupted that is
     * passed.
     */
    private native boolean isInterrupted(boolean ClearInterrupted); //底層ClearInterrupted控制清除與否

4 使用舉例

我們可以使用Thread#interrupt中斷一個執行緒,被中斷的執行緒所受的影響為以下兩種之一:

  • 若被中斷前,該執行緒處於非阻塞狀態,那麼該執行緒的中斷狀態被設為true, 除此之外,不會發生任何事。
  • 若被中斷前,該執行緒處於阻塞狀態(呼叫了wait,sleep,join等方法,那麼該執行緒將會立即從阻塞狀態中退出,並丟擲一個InterruptedException異常,同時,該執行緒的中斷狀態被設為false(作業系統預設操作), 除此之外,不會發生任何事。
 public static void main(String[] args) throws InterruptedException {

        Thread thread = new Thread(()-> {
            try {
                Thread.sleep(7000);
                System.out.println("我醒了 被中斷過 = " + Thread.interrupted());

                System.out.println(Thread.activeCount());

            } catch (InterruptedException e) {
                System.out.println(e);
                e.printStackTrace();
                System.out.println("我被中斷醒了  = " + Thread.interrupted());

            }
        });
        thread.setDaemon(false);
        thread.start();
        // 需要結合blockerLock使用
        thread.interrupt();// 開始之後設定中斷

        System.in.read();
}

// 輸出
我被中斷醒了  = false

5 小結

  1. 中斷異常一般是執行緒被中斷後,在一些block型別的方法(如wait,sleep,join)中丟擲。有些像gc的安全域,安全點。
  2. 當一個執行緒因為呼叫wait,sleep,join方法而進入阻塞狀態後,若在這時中斷這個執行緒,則這些方法將會丟擲InterruptedException異常,我們可以利用這個異常,使執行緒跳出阻塞狀態,從而終止執行緒。
  3. 中斷一個處於執行狀態的執行緒只會將該執行緒的中斷標誌位設為true, 而並不會丟擲InterruptedException異常,為了能在執行過程中感知到執行緒已經被中斷了,我們只能通過不斷地檢查中斷標誌位來實現:
  4. 中斷一個執行緒,只是傳遞了請求中斷的訊息,並不會直接阻止一個執行緒的執行。