Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

What is spurious wakeup  RSS feed

 
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 ???
 
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
 
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 ]
 
Wanderer
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().
 
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
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.
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!