On 27 Nov, 21:14, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
Here is my question: what is wrong with calling wait just as wait()?
I have checked the java web
where it says that wait() can get InterruptedException, but that is
only if another thread interrupts the current thread. There is no way
that thread 1 can interrupt thread 2 that I am aware of. I wonder what
is being referred to here.
Browse to <http://www.google.com/>, type "spurious wakeup"
in the box, and click the "I'm Feeling Lucky" button. Read *all*
of the Wikipedia article this takes you to, not just the part at
the beginning where Butenhof says that there's no such thing.
Done.
Pay
special attention to the "Other reasons for verifying the invariant"
section;
Here it is:
Practical reasons exist for checking the invariant after a return from
a wait other than spurious wakeups. For example, a waked-up thread may
not be scheduled immediately after the wake up, but be at the mercy of
the system scheduler. A scheduler may preempt a process abruptly or
schedule other threads. It may be the case that in the mean time, an
external entity (another process, hardware) has invalidated the
invariant assumption. Wrapping the wait with a loop avoids such cases.
it may not apply to your very simple situation as things
stand today,
You're right, it doesn't.
but what if the next version of this third-party API
grows a few new features? You may as well insure against the
future, when the insurance is as cheap as writing `while' instead
of `if'.
I still don't understand. The wikipedia article is actually talking
about the premature wakeups that can happen when waiting on POSIX
mutexes or condition variables. And these wakeups are due to system
interrupts. I know these might occur in java as well but I figured
this is why wait() throws InterruptedException. Well, if that happens
flow of control is broken and rightly so for my app. I certainly do
not want it to absorb the exception and loop round to wait again.
So that leaves the mysterious spurious wakeup that is referred to in
the paragraph you mention:- "an external entity (another process,
hardware) has invalidated the invariant assumption". I don't see how
hardware can do it without it being handled as InterruptedException so
that only leaves another process. So how can other process cause a
java program to make a thread return from a wait() prematurely. I
really don't see how.
Regards,
Andrew Marlow
As to where the in the JLS spurious wakeups are allowed, I don't know.