java - InterruptedException : what causes it? -


there interesting questions , answers regarding java's interruptedexception, example the cause of interruptedexception , handling interruptedexception in java. however, none of them tells me possible sources of interruptedexception.

what os signals sigterm, sigquit, sigint? pressing ctrl-c on command line produce interruptedexception? else?

none of things list produce interruptedexception.

the thing can interrupt thread call thread#interrupt(). jls relatively clear on matter, section 17.2.3:

17.2.3 interruptions

interruption actions occur upon invocation of thread.interrupt, methods defined invoke in turn, such threadgroup.interrupt.

see the official tutorial on interrupts more info well. specifically:

a thread sends interrupt invoking interrupt on thread object thread interrupted. interrupt mechanism work correctly, interrupted thread must support own interruption.

...

the interrupt mechanism implemented using internal flag known interrupt status. invoking thread.interrupt sets flag. when thread checks interrupt invoking static method thread.interrupted, interrupt status cleared. non-static isinterrupted method, used 1 thread query interrupt status of another, not change interrupt status flag.

by convention, method exits throwing interruptedexception clears interrupt status when so. however, it's possible interrupt status set again, thread invoking interrupt.

the implication is explicit flag settable calling interrupt(), rather triggered other unknown external events. further implied description of exception in various methods throw it, for example (emphasis mine):

interruptedexception - if thread has interrupted current thread. interrupted status of current thread cleared when exception thrown.


the purpose of interrupt system in general provide common, well-defined framework allowing threads interrupt tasks (potentially time-consuming ones) in other threads. while implement similar functionality explicit logic in own application, having well-defined mechanism allows independent classes (e.g. jdk, other third-party code, other independent classes in own code) provide functionality in consistent way.

the many notes , "warnings" see handling interruptedexception aren't meant imply can thrown spontaneously, meant encourage well-designed objects can used in contexts not known yet, interrupt() presumed work (so really, do want assume can thrown spontaneously if creating reusable objects robust in future situations - i.e. you're never guaranteed code won't day used expects interrupts work).

for quick one-off projects don't really need worry special handling these exceptions long know aren't calling interrupt() , aren't calling can call interrupt(), aware of implications of in long run, if end reusing code in other contexts.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -