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

前端 未结 4 1445
我寻月下人不归
我寻月下人不归 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:47

    Rather than catching InterruptedException you can call Thread.interrupted() or Thread.getCurrentThread().isInterrupted() (the former will clear the interrupted flag, the latter will not), which will return true if the interrupted flag is set. You can only catch InterruptedException if you've called a method that throws it, like sleep

提交回复
热议问题