Java interrupt mechanism [reprinted]

Source: Internet
Author: User

After an External Thread calls the thread. Interrupt () method to a thread, the Java language processing mechanism is as follows:

If the thread is in a resumable state. wait (), or selector. select (), thread. sleep () and other specific APIs that will be blocked), the thread will be immediately awakened and will be subject to an interruptedexception. At the same time, if the thread is blocked on Io, the corresponding resource is disabled. If the thread does not execute the "thread. interrupted () method (not interrupt) Next, the thread will shut down these resources when it processes Any Io resources. Of course, the solution is to call interrupted (), but the programmer needs to set it based on the Code logic and determine whether the interrupt can be directly ignored based on their own needs, or Exit immediately.

If the thread is in an uninterrupted state, that is, the above API is not called, Java only sets the thread's interrupt state, and nothing else will happen, if the thread then calls the number of lines to block the API, the thread will immediately jump out and throw interruptedexception. The next thing will be the same as the first one. If you do not call the blocking API, this thread will continue to run. Unless you want to implement such a thread, generally high-performance code will certainly have functions such as wait () and yield () that sell CPU, without the latter situation. Source: bytes) the thread has a function similar to sleep to transfer the CPU. In this way, when the sleep process is interrupted by other threads, interruptedexception will be thrown. In this way, we can handle the catch. For example, continue or break. 2) if there is no function similar to sleep, you can use a while-like check, often check thread. whether interrupted () returns true. If yes, you can make your own predefined logical tasks, such as continue or break. If neither of the preceding methods is available, the thread will run all the time and the returned status of thread. interrupted () can be changed. The thread cannot be stopped.

 

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.