No InterruptedException is not thrown during normal flow, but might happen when interrupt() is called on the thread (e.g. By some other code trying to interrupt normal execution flow of this thread). Normally execution simply continues in the line after the sleep statement.
Up vote 0 down vote favorite 1 share g+ share fb share tw.
Java multithreading interrupt link|improve this question asked May 6 '11 at 7:32ArtWorkAD4,73532479 99% accept rate.
This is, obviously, documented in Thread's javadoc. Why don't you read it to find out? See download.oracle.com/javase/6/docs/api/ja... – JB Nizet May 6 '11 at 7:38 1 you should almost never catch an Exception and just discard it, but you are forgiven as this is only an Example – rurouni May 6 '11 at 7:47 Exceptions should be for exceptional situations.
They should not regularly occur. – Peter Lawrey May 6 '11 at 8:32.
No, InterruptedException is not thrown during normal flow, but might happen when interrupt() is called on the thread (e.g. By some other code trying to interrupt normal execution flow of this thread). Normally execution simply continues in the line after the sleep statement.
If the interrupt method is called during the sleep time. The catch is relevant only for the code of try, after that it has no effect.
InterruptedException is throw if the Thread is interrupted which may happen during the sleep or which might have happened a while ago. In most cases when you do not expect the InterruptedException and don't want to handle it it ist better to try{ sleep(500); }catch(InterruptedException e){ Thread.currentThread().interrupt(); } so the interrupt is not lost.
This is not a good way to handle this case. When a thread is interrupted, it is an indication that the thread should stop. InterruptedException are only thrown when the thread is waiting (sleep, blocking IO operation...).
If the thread is just processing, no exception will be thrown and you are responsible to check the interrupted status on a regular basis to stop processing and let the thread terminate. In your case, your interrupt code is totally useless. – Nicolas Bousquet May 6 '11 at 8:38 @Nicolas: if you are prepared to handle the InterruptedException you are right, but if you are in an helper method which just gets an InterruptedException from an Object.
Wait(int) you have no chance to terminate the Thread. Either your Method throws InterruptedException or you preserve the interrupt state, so the calling code can handle it. – rurouni May 6 '11 at 8:48 Wouldn't be better to just let the exception propagate, or if you want to not deal with checked exception to rethrow an unchecked exception?
– Nicolas Bousquet May 6 '11 at 8:54 It depends, sometimes you need to complete an operation, so immediately throwing the InterruptedException is not possible. Or the common problem that you are implementing an interface which does not allow throwing InterruptedException (e.g. Runnable). But you are right, that after all you should hurry up finishing whatever is to be done when getting interrupted ;-) – rurouni May 6 '11 at 9:44.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.