Thread中interrupt()interrupted()和isInterrupted()的區別

在java線程中,線程的狀態分爲6種。官方文檔的解釋是:java

        /**
         * 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出來。less

RUNNABLE:start方法已運行,全部條件都就緒。只等該線程得到CPU運行時間。this

BLOCKED:等待進入臨界區。線程

WAITING:在使用了wait方法後,等待線程獲取CPU時間。code

TIMED_WAITING:在使用了wait(long),join(long),sleep(long)等帶有限定時間的方法後。orm

TERMINATED:run方法執行完畢。對象

 

interrupt()是對象實例方法blog

在使用了interrupt()後,線程會將中斷標誌設置爲true。若是線程處於waiting或timed_waiting狀態,則會產生一個InterruptedException。ci

isInterrupted()是對象實例方法文檔

在使用isInterrupted()後,若是中斷標誌爲true則返回true。

 

 

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類的靜態方法

第一次使用該方法若是已經中斷,則第二次使用時會取消掉該中斷。

 

這就是這三個方法的不一樣。

 

更多文章:https://blog.gavinzh.com

相關文章
相關標籤/搜索