Java blocking interrupts and Locksupport

Source: Internet
Author: User

Before the introduction, let's throw a few questions.

    1. Thread.Interrupt () method and the relationship of interruptedexception anomalies? is the interruptedexception exception triggered by interrupt?
    2. Thread.Interrupt () will interrupt the thread what state of work? RUNNING or BLOCKING?
    3. General thread programming need to focus on interrupt interrupts? What do you usually do with it? What can be used to do?
    4. Locksupport.park () and Unpark (), the difference from object.wait () and notify ()?
    5. What does the blocker object passed by Locksupport.park (object blocker) do?
    6. Locksupport can respond to Thread.Interrupt () events? Will throw a Interruptedexception exception?
    7. Does the Thread.Interrupt () handle have a corresponding callback function? Similar to a hook call?

If you can be very clear to answer, that you have fully understand thread.interrupt, you can not look down on that.

If it is not clear, with these questions, together to comb under. A few methods of thread interrupt processing:
    • public void interrupt (): Execution Thread Interrupt Event
    • public boolean isinterrupted (): Checks whether the current thread is in interrupt
    • public static Boolean Interrupted (): Check whether the current thread is in interrupt, and reset interrupt information. Similar to Resetandget ()
Understanding: 1. Each thread has a Interrupt StatusFlag bit that indicates whether the current thread is in the interrupt State 2. The general invocation of Thread.Interrupt () will be handled in two ways
    • When encountering a low-priority block state, such as object.wait (), Object.sleep (), Object.join (). It will immediately trigger a unblock to unblock and throw a interruptedexception.
    • In other cases, Thread.Interrupt () Simply updates the status flag. Then your work thread is checked by thread.isinterrrupted () and can do the appropriate processing, such as throw interruptedexception or clean state, cancel task, etc.

Answer some of the previous questions:

Question 1: Thread.Interrupt () method and the relationship of interruptedexception anomalies? is the interruptedexception exception triggered by interrupt?

Answer: Thread.Interrupt () is only in object.wait (). Object.join (), Object.sleep () several methods will actively throw interruptedexception exceptions. And in other blocks common, just by setting the thread of a flag bit information, need to process self-processing.

Question 2: Thread.Interrupt () will interrupt the thread what state of work? RUNNING or BLOCKING?

A: Thread.Interrupt is designed primarily to handle threads in block state, such as wait (), and sleep () status is an example. However, the running state can also be supported when the program is designed to support task cancel. such as Object.join () and some of the NIO channel designs that support interrupt.

Issue 3: General thread programming need to focus on interrupt interrupts? What do you usually do with it? What can be used to do?

A: Interrupt use: unblock operation, support task Cancel, data cleanup and so on.

Question 4: Locksupport.park () and Unpark (), the difference from object.wait () and notify ()?

1. The subject is different. Locksuport is primarily for thread-in blocking processing, you can specify the target object of the blocking queue, each time you can specify a specific thread wake. Object.wait () is based on the object's latitude, blocking the current thread and waking a single (random) or all of the threads.

2. The implementation mechanism is different. Although Locksuport can specify the object objects of monitor, and object.wait (), the blocking queues of both do not intersect. You can look at the test example. Object.notifyall () cannot wake the Locksupport blocking thread.

Question 5: What does the blocker object passed by Locksupport.park (object blocker) do?

A: The corresponding blcoker will be recorded in the thread of a Parkblocker attribute, through the Jstack command can be very convenient to monitor the specific blocking objects.

Question 6: Locksupport can respond to Thread.Interrupt () events? Will throw a Interruptedexception exception?

A: Can respond to interrupt events, but will not throw interruptedexception exceptions.

For links in the original text, see: Https://yq.aliyun.com/articles/14680?spm=5176.100239.yqblog1.108.1I0Dhi

Java blocking interrupts and Locksupport

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.