Object.wait returns normally if interrupted
Alan Bateman
alan.bateman at oracle.com
Fri Jun 27 07:12:17 UTC 2025
On 27/06/2025 03:47, David Holmes wrote:
> Hi Pavel,
>
> On 27/06/2025 8:23 am, Pavel Rappo wrote:
>> Here's an interesting behaviour. A thread that has been blocked in
>> Object.wait is interrupted. But instead of throwing an
>> InterruptedException, Object.wait returns normally with the thread's
>> interrupt status set.
>
> Do you mean it returns normally without being notified? That's allowed
> by spec as it is just a spurious wakeup.
>
> That said, it shouldn't really happen in the current implementation
> AFAIK, though with recent changes around virtual thread support it is
> possible some new edge case has crept in. Or a bug.
>
> If the interrupt is racing with a notification then what you see is
> also allowed as you can't tell which occurred first:
>
> "If the current thread is interrupted by any thread before or while it
> is waiting, ...
>
> once notified it is no longer waiting.
I can't think of any issues. Yes, there was a lot of code changed but it
came with a huge amount of testing and new tests. The tests include
racing notify and interrupt, and also racing notify, timeout and interrupt.
So likely what David suggests. One thread does the notify around the
same time that another thread interrupts the waiter. In that case, it
could easily happen that the waiting thread is awoken (owning he
monitor) and continues at around the time that is is interrupted. It's
also possible to have many threads attempting to enter the monitor, in
which case the waiter is competing.
-Alan
More information about the core-libs-dev
mailing list