Java review-multi-threaded interrupts

Source: Internet
Author: User

1, interrupt in the break process

1, interrupt will throw an exception to the thread in the wait pool, this time the thread will be awakened, but also continue to compete for lock resources, in order to continue to run!

2, interrupt will throw an exception to the thread that is being sleep, but will not throw an exception to the yield thread, the personal feeling is that sleep is specified for how long to block, but yield is not known for at least a long time to block, in addition to yield this method is only used in testing, Less in development.

3. if the thread is calling the wait (), wait (long), or wait (long, int) method of the Object class, or the class's join (), join (long), join (long, int), sleep (long), or Slee When the P (long, int) method is blocked, its interrupt state is cleared and it will also receive a interruptedexception.

4, interrupt Status: Is the thread has been interrupted by an identity, is only a sign! You can view the interrupt state of a thread's running process in the following ways,

1) Interrupted: This is a static method, and this method returns the interrupt state of the current thread, immediately modifies the state to a state that is not interrupted, in other words, if the current thread is interrupted and returns true after calling this method, Call this method again to return is false! In addition, this is a static method (or return the status of the current thread), so it is not recommended to use!

2) isinterrupted: Returns the interrupt state of the current thread and does not modify the state.

In the 3rd, it says that if an exception is thrown, it will be cleared, that is, if the interrupt throws an interrupt exception at this time, but the interrupt status is false, which is never interrupted!


2, the correct thread break

0) Other modes of interruption, such as sleep,yield,join,stop,suspend, should not be a thread break required in a real project!

1) It is obvious that it is not possible to think that the thread can be broken through the interrupt, because this method only modifies the interrupt state of the threads, or the interrupt exception may occur.

2) in the flag method of broken thread, generally in the thread is our own through the flag method of interruption, the following code

public void Run () {while (flag) {//Some codes//modifies the flag = false in a certain condition}}

public void Run () {

while (isinterrupted () | | flag) {

Some codes
Change flag = False
try{
Blocking code
} catch (Interruptedexception e) {
Exception handling Remember to change flag = False because the interrupt status is cleared
}
}

}


Java review-multi-threaded interrupts

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.