
Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
>>>>notifyAll wakes up a larger candidate pool of threads - any of those
>>>>threads that are notified are candidates to enter the 'running' state.
notifyAll causes at most one Thread to move from Thread.State.WAITING to
RUNNABLE (but not immediately, see below). The other waiters will move
to BLOCKED. It is possible that the thread that acquires the lock and
becomes runnable was actually blocked at the start of a synchronize
block, rather than waiting in wait.
>>but only one thread can ever be active inside a synchronized block
>>right. And then to
[quoted text clipped - 9 lines]
> block, the next thread is capable of proceeeding without a second call
> to notify(All). And then the third thread, and the fourth, and so on.
One subtlety I think worth pointing out is that wait releases the lock
(however many times), waits, and then reacquires the lock. notify and
notifyAll are different in that they do not release the lock. You can
put the notify at the top of the synchronize block. Indeed doing so
allows better exception behaviour.
Also note that threads can wake spuriously. So, notify could
theoretically wake more than one thread. And make sure you have that
while loop - I have found shipping, commercial software that did not.
> In practice, Object.wait should only be called within a loop inside of
> the synchronized block, so those threads will check the predicate
[quoted text clipped - 6 lines]
> thread made it true again, so they'll go back to waiting. Nevertheless,
> they do wake up and run some code.
notify instead of notifyAll can give a huge performance improvement. I
have found shipping, commercial software waking an entire waiting
thread-pool of around three hundred threads for each work request.
On the basis of saying what you mean, I prefer notify to notifyAll where
it is applicable. You mean wake one thread, say wake one thread.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Chris Uppal - 12 Dec 2005 10:16 GMT
> notify instead of notifyAll can give a huge performance improvement. I
> have found shipping, commercial software waking an entire waiting
> thread-pool of around three hundred threads for each work request.
>
> On the basis of saying what you mean, I prefer notify to notifyAll where
> it is applicable. You mean wake one thread, say wake one thread.
But using notify() is a good deal harder to get right. Tracking /which/ thread
needs to be notified is tricky what with race-conditions etc. I would
recommend /against/ using notify() unless (as we always say) there is an
actual, measurable, performance problem which has to be fixed. This is more
than just the usual dislike of premature optimisation, since we are not just
talking about the risk of wasting time on needless complexity, but the very
real risk of writing code that will fail mysteriously and irreproducibly.
/You/ may trust youself to get it right -- I don't suggest you shouldn't -- but
I would worry if I saw notify() used in most code-bases.
Anyway, with luck we shall soon all be able to forget that
wait()/notify()/notifyAll() ever existed, and leave the hard work to Doug Lea
and java.util.concurrency.*
-- chris
Chris Smith - 12 Dec 2005 15:22 GMT
> But using notify() is a good deal harder to get right. Tracking /which/ thread
> needs to be notified is tricky what with race-conditions etc.
In fact, clearly it is ALWAYS a bug to use notify() whenever there could
be heterogenous threads waiting on a single monitor. Why? Because you
can't specify which thread to notify; so if you care (that is, if the
threads do different things) then you've done something wrong.
> Anyway, with luck we shall soon all be able to forget that
> wait()/notify()/notifyAll() ever existed, and leave the hard work to Doug Lea
> and java.util.concurrency.*
I'm rather skeptical about this. The new package certainly introduces
tools for solving a few specific common concurrency problems, such as
thread pools and the classic producer/consumer example. However, if I'm
writing concurrent algorithms for matrix operations in a mathematical
computing library, I don't think Doug's done much to help me out.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Chris Uppal - 13 Dec 2005 08:27 GMT
[me:]
> > But using notify() is a good deal harder to get right. Tracking
> > /which/ thread needs to be notified is tricky what with race-conditions
[quoted text clipped - 4 lines]
> can't specify which thread to notify; so if you care (that is, if the
> threads do different things) then you've done something wrong.
This doesn't seem to make sense; am I missing your point, or did you mean
notifyAll() can't be used in such a situation ?
> > Anyway, with luck we shall soon all be able to forget that
> > wait()/notify()/notifyAll() ever existed, and leave the hard work to
[quoted text clipped - 5 lines]
> writing concurrent algorithms for matrix operations in a mathematical
> computing library, I don't think Doug's done much to help me out.
Point taken. But I don't think that concurrent matrix operations are a primary
use-case for less-than-highly skilled programmers. Come to that, I don't
immediately see a lot of need for complex wait-dances in such cases either, not
more than can be handled by the handshake primitives (Barrier, Latch,
Semaphore) that are in there already. But then, I have never paid much
attention to concurrent [array] algorithms, nor really used j.u.c.* yet, so I
may be missing much.
BTW, do you do a lot of concurrent number crunching at DesignACourse ? ;-)
-- chris
vj - 12 Dec 2005 16:59 GMT
>One subtlety I think worth pointing out is that wait releases the lock
>(however many times), waits, and then reacquires the lock. notify and
>notifyAll are different in that they do not release the lock. You can
>put the notify at the top of the synchronize block. Indeed doing so
>allows better exception behaviour.
>Also note that threads can wake spuriously. So, notify could
>theoretically wake more than one thread. And make sure you have that
>while loop - I have found shipping, commercial software that did not.
can you please make you point more clear by giving us a sample code
listing.
Thanks,
---VJ
Chris Smith - 12 Dec 2005 17:59 GMT
> can you please make you point more clear by giving us a sample code
> listing.
Which statement of Thomas's did you want sample code for? Since they're
statements about multithreading and its inherently nondeterministic
nature, it would be tough to demonstrate either one with sample code.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
vj - 13 Dec 2005 04:57 GMT
> Which statement of Thomas's did you want sample code for? Since they're
> statements about multithreading and its inherently nondeterministic
> nature, it would be tough to demonstrate either one with sample code.
Thomas suggested that the wait() call must be encapsulated in a loop
that checks for wakeup conditions. I am requesting a sample that shows
its usage over wait() thats not called from a loop.

Signature
VJ
Chris Smith - 12 Dec 2005 18:03 GMT
> One subtlety I think worth pointing out is that wait releases the lock
> (however many times), waits, and then reacquires the lock. notify and
> notifyAll are different in that they do not release the lock.
Neither notify nor notifyAll releases the lock, so I don't see how this
is a difference. Indeed, if either one did release the monitor, it
would be terribly broken behavior.
> Also note that threads can wake spuriously. So, notify could
> theoretically wake more than one thread.
Indeed. Good point; the intent is for notify to wake only one thread,
but it could wake more. This is consistent with seeing notify as an
optimization of notifyAll, as mentioned earlier in the thread. If it's
too expensive to prevent notify from acting just like notifyAll, then an
implementation could do so and be entirely legal.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
John C. Bollinger - 13 Dec 2005 02:37 GMT
>>One subtlety I think worth pointing out is that wait releases the lock
>>(however many times), waits, and then reacquires the lock. notify and
>>notifyAll are different in that they do not release the lock.
>
> Neither notify nor notifyAll releases the lock, so I don't see how this
> is a difference. [...]
I believe Tom meant that there was a difference between wait() on the
one hand and notify() / notifyAll() on the other. At least, that's the
way I read it.

Signature
John Bollinger
jobollin@indiana.edu