Join,yield understanding of thread interruption

Source: Internet
Author: User

It is dangerous for a thread to be forced to terminate before it ends normally. Because it can bring serious consequences that are completely unforeseen. So you see Thread.Suspend, Thread.stop and so on are deprecated.

So you can't just hang a thread out of it, but sometimes it's necessary to let a thread die or let it end a waiting state. The elegant approach is to give that thread an interrupt signal and let it decide for itself what to do. For example, in a child thread to wait for certain conditions to arrive, you call Thread.Sleep (10000), expect the thread to wake up after 10 seconds of sleep, but if this particular condition arrives early, how do you inform a sleeping thread? For example, the main thread blocks itself by calling the join method of the child thread to wait for the child thread to end, but the child thread finds itself in a short period of time to end, so it needs to find a way to tell the main line Cheng wait for me. In these cases, an interruption is required.
Interrupts are done by calling the Thread.Interrupt () method. This method informs that thread by modifying the interrupt state of the called thread, saying that it was interrupted. For non-blocking threads, only the interrupt state is changed, that is, thread.isinterrupted () will return true; For threads in the unblocking state that can be canceled, such as a thread waiting on these functions, Thread.Sleep (), object.wait (), Thread.Join (), this thread will throw interruptedexception after receiving the interrupt signal, It also resets the interrupt state back to false.

For threads in a unblocking state that is unblocked, it is primarily to wake up the thread so that he does not block and the interrupt state is set to False

For a normal running thread, set the interrupt flag bit to true, and if there is a condition of judgment, tell the judging condition I'm going to break, why do you do it?

Join: A join method that calls another thread in one thread for sequential execution of threads, such as B.join called in thread A, then thread a waits for threads B to run out after executing a.

Yield: Give up the chance of running to another thread, but the current thread may continue to get the opportunity

Join,yield understanding of thread interruption

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.