【JMM】Thread源码分析之线程中断

扫码关注公众号:Java 技术驿站

发送:vip
将链接复制到本浏览器,永久解锁本站全部文章

【公众号:Java 技术驿站】 【加作者微信交流技术,拉技术群】

文章首发于:clawhub.club


线程中断是一个比较重要的概念,可以说操作系统是由中断驱动的。
中断的概念:
是指在程序执行过程中,遇到急需处理的事件时,暂时终止现行程序在CPU上的运行,转而执行相应的事件处理程序,待处理完成后,再返回断点或调度其他程序执行。
JAVA中的线程中断,是不会像操作系统一样处理中断的,因为JAVA中的每个线程,只是保留了中断标志位,要根据标志位来处理中断。

Thread源码中,关于中断的代码:

设置中断

线程设置中断其实就是设置线程中断标识,没有做到暂时终止先行程序在CPU上的运行。

    /**
         * Interrupts this thread.
         * 中断当前线程,其实就是将当前线程设置中断标志位,JAVA设置中断唯一的口子
         *
         * <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() {
            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();
        }
    //底层调用native方法
     private native void interrupt0();

查看中断状态

获取中断状态后,会根据入参来判断是否清除中断标志。

      /**
         * 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);

    /**
         * 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);
        }

        /**
         * 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);
        }

如果想要终止一个线程的执行,最好是在满足一定条件的时候,设置中断,并检查中断,捕捉InterruptedException ,再做处理。


来源:https://www.jianshu.com/p/347ea7f881f8

赞(0) 打赏
版权归原创作者所有,任何形式的转载请联系博主:daming_90:Java 技术驿站 » 【JMM】Thread源码分析之线程中断

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏