【发布时间】:2019-02-14 11:37:58
【问题描述】:
我的代码中有一个中断异常,我正在尝试了解原因。
发生的情况是我试图通过调用 FilesUtils.filesMethod() 在我的文件系统中的某个位置创建文件。
如果我收到 IOException,我会尝试休眠 100 毫秒。 在睡眠期间我需要检查我是否被打断(因为 InterruptedException 是睡眠检查异常)。
如果是这样,我打印“Got interrupted”并将当前线程的中断标志设置为 true。
我的日志中显示“被打断了”。
在我的代码中没有任何地方对该线程的命令 interrput()。
会是什么?
这个场景是否有意义:
ilesUtils.filesMethod() 方法抛出 InterruptedIOException,因为流在线程脚下关闭。此外,此 InterruptedIOException 将 Interrupted 标志设置为 true。
我在我的 catch (IOException e) 中捕获了 InterruptedIOException。
睡眠检查中断标志是否为真。它是(见 1)所以它抛出 InterruptedException。
会是这样吗?
try {
FilesUtils.filesMethod();
break;
} catch (IOException e) {
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e1) {
log.info("Got interrupted", e1);
Thread.currentThread().interrupt();
}
}
【问题讨论】:
标签: java multithreading interrupt interrupt-handling