I want my thread to handle interruption, but I can't catch InterruptedException because it is a checked exception

前端 未结 4 1443
我寻月下人不归
我寻月下人不归 2020-12-11 08:47

I have a thread in Java which calls

t.interrupt();

making t (a different thread) be interrupted. I want the \"t\" thread to then catch an

4条回答
  •  情书的邮戳
    2020-12-11 09:51

    InterruptedException is only thrown from specific method calls - mostly any method calls that wait, sleep or do I/O, so it wont be thrown out of anywhere.

    if the Thread youre trying to interrupt is doing something CPU-internsive and not I/O, you will need to sprinkle Thread.getCurrentThread().isInterrupted() calls in there otherwise the target thread might never notice.

提交回复
热议问题