The difference between interrupt () interrupted () and isinterrupted () in thread

Source: Internet
Author: User

In Java threads, the state of a thread is divided into 6 types. The official documentation is interpreted as:

        /**         *  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,         /**&nBsp;        * 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 just new out.

The Runnable:start method is already running and all conditions are ready. Only wait for the thread to get the CPU run time.

BLOCKED: Wait to enter the critical section.

Waiting: Waits for the thread to get CPU time after using the wait method.

Timed_waiting: After using wait (long), join (long), sleep (long), and so on with a time-limited method.

Terminated:run method execution is complete.


Interrupt () is an object instance method

After interrupt () is used, the thread sets the interrupt flag to true. If the thread is in the waiting or timed_waiting state, a interruptedexception is generated.

Isinterrupted () is an object instance method

Returns true if the interrupt flag is true after using isinterrupted ().



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.

The interrpupted () method is a static method of the thread class

The first time you use this method, if it has been interrupted, the second time you use it, the interrupt will be canceled.


This is the difference between the three methods.


The difference between interrupt () interrupted () and isinterrupted () in thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.