K
K
koi com2014-09-08 19:52:59
Java
koi com, 2014-09-08 19:52:59

What is InterruptedException?

Already googled - but did not understand. Explain with your fingers. I call Thread.sleep () - the compiler asks to catch InterruptedException. I want to know why. javadoc didn't help either

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
z777, 2014-09-09
@koi_jp

Simple enough: Interrupted - translated as interrupted.
Imagine the following situation: there are three threads 1 - the main one, does not depend on anyone, and the other two depend on each other. Let's say the second thread needs data that results from the execution of the third thread. Accordingly, we "put the second thread to sleep" and wait until the third one is executed, but when the third thread is executed, the situation turns out that it is necessary to suspend or interrupt the execution of the second and third threads, respectively, we stop the third thread, and call the second method interrupt () (interrupt). When the thread scheduler tries to resume the second thread, it will not be able to do so, because the second thread is interrupted, that's when this exception is obtained. The example is not very successful, please do not judge strictly.

I
igor_suhorukov, 2014-09-18
@igor_suhorukov

With one little note

If you catch an InterruptedException but cannot rethrow it, then you must keep proof that the interrupt occurred so that code up the call stack can learn about the interrupt and react to it if it wants to. This task is done by calling interrupt() to "re-interrupt" the current thread

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question