Java multithreading in the use of thread (Interrupt) detailed _java

Source: Internet
Author: User
Tags sleep

Interrupt method

Interrupt is literally meant to be interrupted, but in Java the Thread.Interrupt () method actually notifies the thread in some way and does not abort the thread directly. What you do is determined by the person who writes the code, and usually we abort the thread.

If the thread is calling a wait (), a wait (long), or a wait (long int) method of the object class, or a join (), join (long), join (long, int), sleep (long), or sleep (long , int) method is blocked, its break state will be cleared and it will receive a interruptedexception.

If the thread is blocked in an I/O operation on an interruptible channel (Java.nio.channels.InterruptibleChannel), the channel is closed, the interrupt state of the thread is set, and the thread receives a Closedbyinterruptexception.

If the thread is blocked in a Selector (Java.nio.channels.Selector), the interrupt state of the thread is set, and it will immediately return from the select operation and may have a value of not 0 as if the wakeup method of the selector was invoked.

If none of the previous conditions were saved, the interrupt state of the thread is set.

Interrupting a thread that is not active does not require any action.

Detect interrupts

How to detect interrupts depends on what the thread does.

If the thread calls a method that can throw a interruptexception, the interruptexception is captured and processed in a catch block (usually by exiting the Run method to medium thread)

If other methods are invoked, the thread.interrupted can be checked at idle time to determine whether the interrupt signal has been received and processed after the interrupt signal is received. You can throw a interruptexception so that it's consistent with the previous approach

Break status

The thread's interrupt mechanism is implemented using the internal flag of the interrupt state. The interrupt state is set when the interrupt () method of the thread is invoked (refer to the Interrupt method description above).

There are two ways to get the interrupt state of a thread:

Calls the static method Thread.interrupted (), which clears the interrupt state of the current thread in addition to returning the interrupt state of the current thread. In other words, if the method is called twice in a row, the second call returns FALSE, except if the first call has cleared its break state, and the second call is interrupted before the interrupt state is checked out.

Invokes the isinterrupted () method of the specified thread, which returns only the interrupt state of the specified thread without affecting the interrupt state of the thread.

There are two ways to clear the interrupt state of a thread:

    calls thread.interrupted ()

    calls the thread's interrupt () as described above. Method throws a interruptedexception, it clears the interrupt state of the thread at the same time, including the object class Wait (), the Wait (long), or the wait (long, int) method, or the thread's join (), join ( Long), join (long, int), sleep (long), or sleep (long, int) methods

Related Article

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.