• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

What is spurious wakeup

 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone i just came across this term "spurious wakeup" in context of waiting thread... What does it mean ???
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it.. Thanks Peter
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic