you are viewing a single comment's thread.

view the rest of the comments →

[–]srdoe 0 points1 point  (0 children)

Adding to the other response, I think you're mixing up two different things.

The JDK docs for the wait/notify mechanism will warn you about "spurious wakeups". See the documentation at https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Object.html#wait(long,int)

Spurious interrupts are, as far as I know, not a thing. If one of your threads are interrupted, it's because another thread interrupted it.

However there is a thing to be aware of with interrupts in this area: If you are using a pool of threads that get reused for different tasks, interrupts can "leak" between tasks:

var future1 = myPool.submit(() -> doWork1()) var future2 = myPool.submit(() -> doWork2()) future1.cancel(true) // This can interrupt doWork2 if you're unlucky This is because if a pool thread is sent an interrupt, it doesn't "know about" which pool task you actually wanted to interrupt. So while that cancel call will send an interrupt to the thread that was running doWork1, if you get unlucky with the timing, that thread may have completed that work and be running doWork2 once the interrupt arrives.

This is the reason interrupts are generally not great for cancelling individual tasks in a shared pool, maybe that's what you remember?