• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is spurious wakeup

 
abhishek pendkay
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone i just came across this term "spurious wakeup" in context of waiting thread... What does it mean ???
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes, one might have several threads waiting on the same object. Those threads may be interested in different aspects of that object.

Say one thread does a notifyAll() on an object, to notify that there have been changes to one particular aspect. All waiting threads will wake up, but only some of them will be interested in the aspect that has changed; the remainder will have experienced a "spurious wake-up".

Typically, a loop is used, to re-wait after a spurious wake-up. The loop should exit only when, after waking, the thread determines that the aspect it was interested in has changed.
 
abhishek pendkay
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it.. Thanks Peter
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:

Say one thread does a notifyAll() on an object, to notify that there have been changes to one particular aspect. All waiting threads will wake up, but only some of them will be interested in the aspect that has changed; the remainder will have experienced a "spurious wake-up".

Well, according to the java language specifications, this is not what they term as the spurious wakeup. This is infact the required semantics of the notifyAll() method. However, this is one of the reasons why the wait() calls must be inside a conditional loop.
The spurious wakeups are permitted(though not encouraged) for jvm implementations, wherein, they can remove the threads from the object's wait set without any explicit instructions to do so i.e no Interrupt, wait timeout and notify/notifyAll().
[ October 31, 2007: Message edited by: Nitesh Kant ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To put it another way, a thread may wake up from wait() for no reason at all - with no notify(0 or notifyAll(). So you should always have a loop to check whether whatever-thing-you're-waiting-for has actually occurred yet or not, and if not, resume the wait().
 
Val�ry Urbain
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
To put it another way, a thread may wake up from wait() for no reason at all - with no notify(0 or notifyAll(). So you should always have a loop to check whether whatever-thing-you're-waiting-for has actually occurred yet or not, and if not, resume the wait().

Totally agree. Also Doug Lea notes in his book "Concurrent Programming in Java 2nd Edition":
"As of this writing, the JLS does not specifically acknowledge that spurious wakeups may occur. However, many JVM implementations are constructed using system routines (for example POSIX threads libraries) in which spurious wakeups are permitted and are known to occur."
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to further clarify: spurious wakeups were not acknowledged in the first or second editions of the Java Language Specification, which is why Lea says "as of this writing...". The third edition did finally fix this, and that's what Nitesh quotes above.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!