简体   繁体   中英

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().

If I get an IOException I try to sleep for 100 ms. During the sleep I need to check if I am interrupted (because InterruptedException is a check exception with sleep).

if so I print "Got interrupted" and get the interrupt flag of the current thread to true.

I am getting "Got interrupted" printed to my log.

Nowhere in my code there is the command interrput() to this thread.

what could it be?

could this senario makes sense:

  1. the method of ilesUtils.filesMethod() throws InterruptedIOException because the stream was closed under the threads feets. Also this InterruptedIOException set the Interrupted flag to true.

  2. I catch InterruptedIOException in my catch (IOException e).

  3. The sleep checks if Interrupted flag is true. it is (see 1) so it throws 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. But just throwing InterruptedIOException does not cause interrupt flag to be set.

I am guessing FilesUtils.filesMethod() sets the interrupt flag before throwing the InterruptedIOException and then sleep method throws InterruptedException (because interrupt flag is set).

You can clear the interrupt flag with Thread.interrupted() before calling sleep method (if You want to be able to sleep in this case). So sleep method does not throw InterruptedException .(If the thread does not get interrupted again)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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