简体   繁体   English

InterruptedIOException 是否将线程的 Interrupted 标志设置为 true?

[英]Does InterruptedIOException set the Interrupted flag of the thread to true?

I am having an interrupted exception in my code and I am trying to understand why.我的代码中有一个中断的异常,我试图理解为什么。

What happens is that I am trying to create files in some place in my file system by calling FilesUtils.filesMethod().发生的情况是我试图通过调用 FilesUtils.filesMethod() 在我的文件系统中的某个地方创建文件。

If I get an IOException I try to sleep for 100 ms.如果我收到 IOException,我会尝试休眠 100 毫秒。 During the sleep I need to check if I am interrupted (because InterruptedException is a check exception with sleep).在睡眠期间,我需要检查是否被中断(因为 InterruptedException 是睡眠检查异常)。

if so I print "Got interrupted" and get the interrupt flag of the current thread to true.如果是这样,我打印“Got interrupted”并将当前线程的中断标志设置为true。

I am getting "Got interrupted" printed to my log.我的日志中打印了“被打扰了”。

Nowhere in my code there is the command interrput() to this thread.在我的代码中没有任何地方有这个线程的命令 interrput() 。

what could it be?会是什么呢?

could this senario makes sense:这个 senario 是否有意义:

  1. the method of ilesUtils.filesMethod() throws InterruptedIOException because the stream was closed under the threads feets. ilesUtils.filesMethod() 的方法抛出 InterruptedIOException 因为流在线程脚下关闭。 Also this InterruptedIOException set the Interrupted flag to true.此外,此 InterruptedIOException 将 Interrupted 标志设置为 true。

  2. I catch InterruptedIOException in my catch (IOException e).我在 catch (IOException e) 中捕获了 InterruptedIOException。

  3. The sleep checks if Interrupted flag is true.睡眠检查中断标志是否为真。 it is (see 1) so it throws InterruptedException.它是(见 1)所以它抛出 InterruptedException。

could this be the case?会是这样吗?

        try {
            FilesUtils.filesMethod();
            break;
        } catch (IOException e) {
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e1) {
                log.info("Got interrupted", e1);
                Thread.currentThread().interrupt();
            }
        }

Without knowing the implementation details of Your FilesUtils.filesMethod() , We can only guess what is happening here.在不知道 Your FilesUtils.filesMethod()的实现细节的情况下,我们只能猜测这里发生了什么。 But just throwing InterruptedIOException does not cause interrupt flag to be set.但只是抛出InterruptedIOException不会导致设置中断标志。

I am guessing FilesUtils.filesMethod() sets the interrupt flag before throwing the InterruptedIOException and then sleep method throws InterruptedException (because interrupt flag is set).我猜FilesUtils.filesMethod()在抛出InterruptedIOException之前设置了中断标志,然后sleep方法抛出InterruptedException (因为设置了中断标志)。

You can clear the interrupt flag with Thread.interrupted() before calling sleep method (if You want to be able to sleep in this case).您可以在调用 sleep 方法之前使用Thread.interrupted()清除中断标志(如果您希望在这种情况下能够休眠)。 So sleep method does not throw InterruptedException .(If the thread does not get interrupted again)所以 sleep 方法不会抛出InterruptedException 。(如果线程没有再次中断)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM