When we are talking about interrupts in java, say, Thread.sleep(1000);
it might throw an exception only if it's been called t.interrupt()
or also for potential interruptions sent by the OS?
Asked
Active
Viewed 84 times
2

Rollerball
- 12,618
- 23
- 92
- 161
-
Note that some frameworks such as ExecutorServices can call interrupt in some circumstances (task cancellation, shutdown...). See also: http://stackoverflow.com/questions/2126997/who-is-calling-the-java-thread-interrupt-method-if-im-not – assylias Jun 01 '13 at 08:52
1 Answers
2
From the Java 7 language specification:
Interruption actions occur upon invocation of
Thread.interrupt
, as well as methods defined to invoke it in turn, such asThreadGroup.interrupt
.
There is no mention of external factors being able to interrupt a thread.

Thomas
- 174,939
- 50
- 355
- 478
-
mm.. What if the the OS needs to do something critical and need CPU in that moment? would not send an interrupt to the current thread(without terminating it)? – Rollerball Jun 01 '13 at 08:30
-
No. It just deschedules the entire process. Thread interruption has nothing to do with either thread scheduling or process scheduling. – Thomas Jun 01 '13 at 08:33
-
Unfortunately, the word 'Interrupt' is overloaded. It was bad choice for this Java functionality since drivers and OS had already had this word 'booked up' for decades. – Martin James Jun 01 '13 at 09:38
-
@MartinJames ok thanks a lot I thought it was the same thing! it's just an overloading. damn! – Rollerball Jun 01 '13 at 10:33