美文网首页
Thread类中interrupt()、interrupted(

Thread类中interrupt()、interrupted(

作者: 陈萍儿Candy | 来源:发表于2020-10-27 10:24 被阅读0次

    1.interrupt()给创建的线程设置一个中断标志位
    isInterrupted() 线程是否被中断
    interrupted() 当前的线程是否被中断,并清除中断标识位(这个是运行的当前的线程,不是创建的线程)

    测试代码如下,在activity的oncreate方法中,创建thread1线程

    lockObject = new LockObject();
            thread1 = new ThreadTest1(lockObject);
            thread1.setPriority(5);
            thread1.start();
          // thread1线程设置了中断
            thread1.interrupt();
            // 打印的结果是true,thread1有中断标识
            Log.i(TAG, "onCreate: "+thread1.isInterrupted());
            // 因为是在acitvity的oncreate方法中调用thread1.interrupted(),当前线程是main线程,没有设置中断标识,返回为false
            Log.i(TAG, "onCreate: "+thread1.interrupted());
    

    ThreadTest1代码

    public class ThreadTest1 extends Thread{
        private LockObject lockObject ;
    
        public ThreadTest1(LockObject lockObject) {
            this.lockObject = lockObject;
        }
    
        @Override
        public void run() {
            super.run();
          // 这个线程是否被中断    由上面代码可知返回为true
            Log.i("cyp", "run: "+this.isInterrupted());
            if (isInterrupted()) {
                // 当前线程是否被中断,返回为true,并清除当前线程的终端标识。
                Log.i("cyp", "run: "+this.interrupted());
            }
            // 终端标识被清除,返回为false
            Log.i("cyp", "run: "+this.isInterrupted());
        }
    }
    
    

    结论:interrupt()不能中断在运行中的线程,它只能改变中断状态而已。ThreadTest1的run方法正常执行完;
    但是可以根据isInterrupted()方法返回的线程中断状态,可以做不同的操作

      @Override
        public void run() {
            super.run();
          // 这个线程是否被中断    由上面代码可知返回为true
            Log.i("cyp", "run: "+this.isInterrupted());
            if (isInterrupted()) {
                // 当前线程是否被中断,返回为true,并清除当前线程的终端标识。
                Log.i("cyp", "run: "+this.interrupted());
                return;
            }
            // 终端标识被清除,返回为false
            Log.i("cyp", "run: "+this.isInterrupted());
        }
    

    2.这三个方法的源码

    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() {
            if (this != Thread.currentThread())
                checkAccess();
    
            synchronized (blockerLock) {
                Interruptible b = blocker;
                if (b != null) {
                    nativeInterrupt();
                    b.interrupt(this);
                    return;
                }
            }
            nativeInterrupt();
        }
    

    isInterrupted();

      /**
         * 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
         */
        @FastNative
        public native boolean isInterrupted();
    

    interrupted();清除中断状态
    return 如果当前线程被中断,返回为true,否则为false

      /**
         * 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
         */
        @FastNative
        public static native boolean interrupted();
    

    相关文章

      网友评论

          本文标题:Thread类中interrupt()、interrupted(

          本文链接:https://www.haomeiwen.com/subject/lkaamktx.html