What kind of behaviour causes an interrupted exception?

后端 未结 7 767
说谎
说谎 2020-12-24 13:15

I\'m relatively new to Threading in Java and I\'ve noticed that everytime I use Thread.sleep() I have to catch InterrupetdException.

What kind of behaviour causes th

7条回答
  •  天命终不由人
    2020-12-24 13:36

    Some advices from Java Concurrency in Practice:

    • Propagate the exception (possibly after some task-specific cleanup), making your method an interruptible blocking method, too; or
    • Restore the interruption status so that code higher up on the call stack can deal with it.
    • Only code that implements a thread's interruption policy may swallow an interruption request. General-purpose task and library code should never swallow interruption requests.

提交回复
热议问题