InterruptedException

eric at kolotyluk.net eric at kolotyluk.net
Wed Dec 22 19:07:48 UTC 2021


*	or  <https://download.java.net/java/early_access/loom/docs/api/java.base/java/lang/Thread.html#sleep(long,int)> sleep(long, int) methods of this class, then its interrupt status will be cleared and it will receive an  <https://download.java.net/java/early_access/loom/docs/api/java.base/java/lang/InterruptedException.html> InterruptedException.

 

Ahhhh, thanks…

 

So, because I am pinned by sleep(), the flag gets reset… got it now… How have I been so wrong for so many years?

 

I know people like Javadoc to be minimalist, but it would still help to clarify this in 

<https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/InterruptedException.html>

 

Doesn’t solve my problem of why my thread is ending prematurely, but I will investigate further.

 

Cheers, Eric



 

From: Alex Otenko <oleksandr.otenko at gmail.com> 
Sent: December 22, 2021 10:59 AM
To: Eric Kolotyluk <eric at kolotyluk.net>
Cc: loom-dev <loom-dev at openjdk.java.net>
Subject: Re: InterruptedException

 

Please check java docs for Thread.interrupt()

 

Alex

 

On Wed, 22 Dec 2021, 18:52 Eric Kolotyluk, <eric at kolotyluk.net <mailto:eric at kolotyluk.net> > wrote:

I am embarrassed to bring this up here, but I cannot solve this little
puzzle

public Duration sleep(Consumer<InterruptedException> exceptionHandler) {
    final var duration = getDuration();
    try {
        Thread.sleep(duration);
    } catch (InterruptedException interruptedException) {
        if (exceptionHandler == null)
            System.out.println("Lag: ignoring interrupt, interrupted =
" + Thread.currentThread().isInterrupted());
        else
            exceptionHandler.accept(interruptedException);
    }
    finally {
        return duration;
    }
}

When I do a thread.interrupt() I see the following output

Lag: ignoring interrupt, interrupted = false

where I expected interrupted = true and eventually the thread does stop
prematurely, where my intention was that the interrupt just be ignored...Is
the call to println() stopping the thread prematurely? I discovered this
interesting scenario while writing unit tests for my Lag API.

For decades my understanding of catching an InterruptedException was that
it *did not* reset the isInterrupted() flag.

There is nothing in Class InterruptedException
<https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/InterruptedException.html>
that indicates that just catching the exception changes the status of this
flag. Could someone please clarify this in the JavaDoc.

Or maybe my misunderstanding goes deeper, but my intuition has really
failed me here... I was going to test this with Virtual Threads, but I
wanted to understand Platform Threads better first...

Sometimes reasoning about concurrency hurts my brain...

Cheers, Eric



More information about the loom-dev mailing list