Seventh chapter cancellation and closure
7.1 Task Cancellation
Method One, the volatile type of the domain to save the cancellation status
Mode two, interrupt () method
Interrupt () can interrupt the target thread
The isinterrupted () method is used to detect the interrupt state of the target thread
Interrupted () is used to clear the interrupt state and return the previous interrupt state, which is the only way to clear the interrupt state, and if you call the method to return true, then unless you want to block the interrupt, you have to deal with it, can throw interruptexeption exception or re-interrupt to resume interrupt state
Sleep (), wait (), join () returns early when an interrupt is found, returns an Interruptexception exception, and clears the interrupt state.
How to respond to interrupts:
If you encounter an interruptible blocking method that throws Interruptedexception, you can continue to throw the exception to the upper layer of the method call stack, and if an interrupt is detected, you can clear the interrupt state and throw interruptedexception. Makes the current method also an interruptible method.
If sometimes it is inconvenient to throw interruptedexception on the method, for example, the method signature on an interface to be implemented does not have throws interruptedexception on it, You can then capture the interruptedexception of the Interruptible method and reset the interrupt state through Thread.currentThread.interrupt (). This is true if the interrupt state is detected and cleared.
In general code, especially as a base class library, you should never swallow interrupts, that is, after capturing to Interruptedexception, do nothing in the catch, clear the interrupt state without resetting the interrupt state or throw interruptedexception and so on. Because swallowing the interrupt state causes the upper layer of the method call stack to get no such information.
Of course, there are always exceptions to this, when you know exactly who your method will be called, and the caller will not get into trouble because the interruption is swallowed up.
It's all about getting the upper layer of the method call stack to learn about the break. Suppose you write a class library that has a method Amethod in the class library, detects and clears the interrupt state in Amethod, and does not throw the interruptedexception, as the Amethod user, he does not know the details, If the user is going to use interrupts to do something after calling Amethod, he will never detect an interruption after calling Amethod, because the interrupt information has been removed by Amethod. What if, as a user, you encounter such a problematic class library and cannot modify the code? I had to set a state of my own in my class, and when I called the interrupt method, I set the state at the same time, which is a way to use it without a way.
What if the task does not respond to interrupts? Resolve by setting the delay for join.
Cancel with future
How do I cancel non-disruptive blocking? Overriding the Interrupt method
7.2 Stopping a thread-based service
The thread's stop should be executed by its owner.
Java Concurrent Programming Practical learning notes cancellation and closure