Thread中interrupt()interrupted()和isInterrupted()的区别

2020-04-28 17:44:21 浏览数 (2)

在java线程中,线程的状态分为6种。官方文档的解释是:

代码语言:javascript复制
/**
         * Thread state for a thread which has not yet started.
         */
        NEW,

        /**
         * Thread state for a runnable thread.  A thread in the runnable
         * state is executing in the Java virtual machine but it may
         * be waiting for other resources from the operating system
         * such as processor.
         */
        RUNNABLE,

        /**
         * Thread state for a thread blocked waiting for a monitor lock.
         * A thread in the blocked state is waiting for a monitor lock
         * to enter a synchronized block/method or
         * reenter a synchronized block/method after calling
         * {@link Object#wait() Object.wait}.
         */
        BLOCKED,

        /**
         * Thread state for a waiting thread.
         * A thread is in the waiting state due to calling one of the
         * following methods:
         * <ul>
         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
         *   <li>{@link #join() Thread.join} with no timeout</li>
         *   <li>{@link LockSupport#park() LockSupport.park}</li>
         * </ul>
         *
         * <p>A thread in the waiting state is waiting for another thread to
         * perform a particular action.
         *
         * For example, a thread that has called <tt>Object.wait()</tt>
         * on an object is waiting for another thread to call
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
         * that object. A thread that has called <tt>Thread.join()</tt>
         * is waiting for a specified thread to terminate.
         */
        WAITING,

        /**
         * Thread state for a waiting thread with a specified waiting time.
         * A thread is in the timed waiting state due to calling one of
         * the following methods with a specified positive waiting time:
         * <ul>
         *   <li>{@link #sleep Thread.sleep}</li>
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
         *   <li>{@link #join(long) Thread.join} with timeout</li>
         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
         * </ul>
         */
        TIMED_WAITING,

        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;

NEW:Thread刚刚new出来。

RUNNABLE:start方法已运行,所有条件都就绪。只等该线程获得CPU运行时间。

BLOCKED:等待进入临界区。

WAITING:在使用了wait方法后,等待线程获取CPU时间。

TIMED_WAITING:在使用了wait(long),join(long),sleep(long)等带有限定时间的方法后。

TERMINATED:run方法执行完毕。

interrupt()是对象实例方法

在使用了interrupt()后,线程会将中断标志设置为true。如果线程处于waiting或timed_waiting状态,则会产生一个InterruptedException。

isInterrupted()是对象实例方法

在使用isInterrupted()后,如果中断标志为true则返回true。

代码语言:javascript复制
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.

interrpupted()方法是Thread类的静态方法

第一次使用该方法如果已经中断,则第二次使用时会取消掉该中断。

这就是这三个方法的不同。

0 人点赞