• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why call await() inside a while loop instead of inside an if

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I'm doing a bit of reading on the new JDK5 concurrency package. My question is:

Why do I have to do:



why can't I do this in an 'if':



As far as I can see, when another thread has fulfilled the condition which the blocked thread is waiting in (with signalAll()) then (providing that thread is aware of the okToProceed condition which it could do if both threads were under control of the same programmer) the 'if' construct would be good enough.

Is this just a belt-and-braces "you may aswell retest the expression again just to be on the safe side since you've already got the lock anyhow" test or am I missing something here?


Thanks in advance,
Alistair
 
Alistair Parler
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just had an idea:

is it because signalAll() will tell all threads in the wait-set to become runnable and therefore it's possible that one of the threads which was signalled has modified the state so that it's possible that it will spoil it for other threads? If that makes sense... Would we therefore not have the problem if we just used signal() to wake one thread (i.e. we could use the 'if' if we only used signal() from the condition-enabling thread)
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pay attention to "spurious wakeup":

A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:

synchronized (obj) {
while (<condition does not hold>)
obj.wait(timeout);
... // Perform action appropriate to condition
}



from:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#wait()

and:
http://java.sun.com/javase/7/docs/api/java/util/concurrent/locks/Condition.html#await()

Adam
 
Alistair Parler
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Adam - that clears things up for me!
 
reply
    Bookmark Topic Watch Topic
  • New Topic