Java Interrupt Interrupt Detailed

Source: Internet
Author: User

Interrupt Brief Introduction

The interrupt () method simply changes the state of the interrupt, and it does not interrupt a running thread. What this method actually does is to send an interrupt signal to the blocked thread so that the blocked thread exits the blocked state. More precisely, if the thread is blocked by one of the three methods of Object.wait, Thread.Join and Thread.Sleep, and the Interrupt () method of the thread is invoked, the thread throws a The Interruptedexception interrupt exception (the thread must be prepared to handle this exception beforehand) to prematurely terminate the blocked state. If the thread is not blocked, then calling interrupt () will not work until it is executed to wait (), sleep (), join (), and the interruptedexception is thrown immediately.

When thread a executes sleep,wait,join, thread B invokes the interrupt method of thread A, and indeed this time a Interruptedexception exception is thrown out. But this is actually in the sleep,wait,join these methods will constantly check the value of the interrupt state, and its own throw interruptedexception. If thread A is performing some of the specified actions, such as values, For,while,if, calling methods, and so on, and does not check the interrupt state, thread A does not throw the interruptedexception and always performs its own operation.

Attention:

When thread a executes to wait (), sleep (), join (), when the interruptedexception is thrown, the interrupt state has been reset by the system, and thread A calls thread.interrupted () to return false.

If the thread is called interrupt (), when the thread is not waiting (), sleep (), join (), the next time you execute Wait (), sleep (), join (), the same throws the Interruptedexception, Of course, the interrupt state of the thread will be reset by the system after it is thrown.

(Summary: Call the Interrupt () method, immediately change the state of interruption, but if not in the blocking state, it will not throw an exception; If the interrupt status is interrupted after entering the blocking state, the exception is thrown immediately)

1. Sleep () &interrupt ()

Thread A is using sleep () pause: Thread.Sleep (100000), if you want to cancel its wait state, you can invoke A.interrupt () [A is the thread instance that threads a corresponds to] in the executing line Chengri (for example, here is B). Cause thread A to give up the sleep operation. That is, executing a.interrupt () in thread B, threads A in blocking will give up the sleep operation.

When the thread is called interrupt () while in sleep, it immediately discards the paused state and throws the interruptedexception. That throws an exception is a thread.

2. Wait () &interrupt ()

Thread A called wait () to enter the waiting state, or it can be canceled with interrupt (). However, you should pay attention to the problem of locking. The thread is in the waiting area, the lock is lifted, and when interrupt () is invoked on the waiting thread, the lock is retrieved and the exception is thrown. The exception cannot be thrown until the lock is acquired.

3. Join () &interrupt ()

When a thread waits for the other thread to end with a join (), when it is called interrupt (), it jumps to the catch block as soon as sleep () ...

Note that the interrupt () method invoked must be the interrupt method that invokes the blocked thread. Call Thread T.interrupt (), in thread A.

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.