Java multi-thread interrupt thread (Interrupt) Usage Details

Source: Internet
Author: User

Interrupt Method

Interrupt literally means interruption, but in Java, the thread. Interrupt () method actually notifies the thread in some way and does not directly stop the thread. Write specific tasksCodeUsually we will stop this thread.

If the thread is calling the wait (), wait (long), or wait (long, INT) Methods of the object class, or the join (), join (long), join (long, when the INT), sleep (long), or sleep (long, INT) method is blocked, the disconnection state is cleared and an interruptedexception is received.

If the thread is in an interrupted channel (Java. NIO. channels. interruptiblechannel.

If the thread is in a selector (Java. NIO. channels. selector), the thread's interruption status will be set, it will immediately return from the selection operation, and may have a non-zero value, just like calling the selector's Wakeup method.

If none of the preceding conditions are saved, the thread interruption status is set.

Interrupting a thread that is not in the active State does not require any function.

Detection interruption

How to detect interruptions depends on what the thread does.

If the thread call can throw the interruptexception method, capture interruptexception and process it in the Catch Block (usually exit the run method to interrupt the thread)

If other methods are called, you can check thread. interrupted in idle time to determine whether the interrupt signal is received and process it after the interruption signal is received. You can throw an interruptexception to be consistent with the previous method.

Interruption status

The thread interrupt mechanism is implemented using the internal mark of the interrupt status. The interrupt status is set when the thread's interrupt () method is called (refer to the preceding interrupt method description ).

There are two ways to get the thread interruption status:

Call the static method thread. interrupted (). In addition to returning the interruption status of the current thread, This method also clears the interruption status of the current thread. In other words, if this method is called twice in a row, false will be returned for the second call (after the first call has been cleared, and the second call has completed the interruption state, except when the current thread is interrupted again ).

Call the isinterrupted () method of the specified thread. This method returns only the interrupt status of the specified thread without affecting the interrupt status of the thread.

There are two ways to clear the thread interruption status:

Call thread. interrupted ()

When the interrupt () method of this thread is called to cause interruptedexception, the thread interruption status is cleared, including the wait (), wait (long), or wait (long, INT) method, or the join (), join (long), join (long, INT), sleep (long), or sleep (long, INT) method of the 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.