Java – when should a method throw an interruptedexception, and how should I handle one? (blocking method)
If a method must be a blocking method, do I think correctly if I leave
In short:
>A blocking method should include throws interruptedexception, otherwise it is an ordinary method. > Blocking methods may affect response speed because it may be difficult to predict when to complete, which is why you need to throw interruptedexception
Is that right?
Solution
No, I didn't find that your summary is correct Usually, if you are writing a method to call others to throw interruptedexception, your method should also throw the thrown interruptedexception, unless you have a good plan when your method depends on signal interruption
It is rare for you to be able to absorb such interruptions Maybe you are calculating an iterative solution, and the accuracy increases over time, but when your thread is interrupted, you decide that the solution reached within the specified time is good enough and still enough to return In other words, the solution is still within your approach
Imagine:
private double improveUpon(double start) throws InterruptedException { // ... } public double compute() { double result = 0.0; try { do { result = improveUpon(result); } while (CouldBeImproved(result)); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } return result; }
Alternatively, if you only want to comply with the interrupt request, you can do this without executing interruptedexception:
private double improveUpon(double start) { // ... } public double compute() { final Thread current = Thread.currentThread(); double result = 0.0; do { result = improveUpon(result); } while (CouldBeImproved(result) && !current.isInterrupted()); return result; }
For another variant, consider the situation where your method must do all its work or indicate to the caller that it cannot do it, and it will take some time to arrive, but you want to follow thread interrupts Such a thing is enough
private double improveUpon(double start) { // ... } public double compute() throws InterruptedException { final Thread current = Thread.currentThread(); double result = 0.0; do { if (current.interrupted()) throw new InterruptedException(); result = improveUpon(result); } while (!isAdequate(result)); return result; }
Note that we call thread #interrupt (), which has the side effect of clearing the interrupt state of the thread (if set) If the method returns true, we, as callers, have accepted the responsibility of saving and communicating the interrupt state In this case, because we do not assume that we have created a calling thread, we do not have enough visible range to understand its interrupt strategy, so we throw interruptedexception to convey the interrupt state we observe and adopt
Marking a method as "blocked" is always a matter of degree; Each method blocks its caller for a period of time The difference you may be looking for is whether the method prevents waiting for some external input, such as user keys or messages arriving over the network In this case, you throw an advertisement for interruptedexception to show your caller that your method can safely use the caller of the thread to control its latency You're saying, "it may take a while to complete, but it's no better than you're willing to wait." You're saying, "I'll run until you tell me not to. This is different from Java. Io. InputStream #read(), that is, it's possible to block before three situations occur, otherwise the caller's thread won't be interrupted
In most cases, your decision is to answer the following questions:
>In order to meet the requirements of my method, do I need to call any method that throws interruptedexception? > If so, I have completed any use of my caller? > If not, I should throw interruptedexception. > If I do not call throws interruptedexception, should I respect the interrupt status of my calling thread? > If so, has any work been done and I find that I have disturbed any use of my caller? > If not, I should throw interruptedexception
When a person detects that the current thread is interrupted and swallowed, it is usually limited to the case that your author creates a problematic thread, and once the thread is interrupted, you have committed to exiting the thread's run () method This is the concept of "cooperative cancellation". You can observe the request of your thread to stop running, and you decide to comply with the request, complete your work as soon as possible, and expand the call stack of the thread Thirdly, unless you are the author of the thread's run () method, swallowing the thread's interrupt state may damage the expected behavior of callers and other methods they call
I suggest you study the topic of thread interrupt status and use the methods thread #isinterrupted(), thread #interrupted() and thread #interrupt() Once you understand this and see that the running interruptedexception is an alternative to thread #isinterrupted (), indicating that it returns true or that the polite translation of thread #interrupt () has returned true, all this should begin to make more sense
If you need more learning examples, please say so, and I can add suggestions here