Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > calling wait outside of loop

Reply
Thread Tools

calling wait outside of loop

 
 
puzzlecracker
Guest
Posts: n/a
 
      05-07-2006
The argument is that calling wait prior to loop me cause a thread never
to be aweken (given that notify[all] has been called prior)

How does waiting in the loop prevent that:

while(a condition){
obj.wait();

}
and notify has been called prior to wait, indefinite sleep is
guaranteed.

what is the argument for calleding after the loop.

thanks

 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      05-07-2006
puzzlecracker wrote:
> The argument is that calling wait prior to loop me cause a thread never
> to be aweken (given that notify[all] has been called prior)
>
> How does waiting in the loop prevent that:
>
> while(a condition){
> obj.wait();
>
> }
> and notify has been called prior to wait, indefinite sleep is
> guaranteed.
>
> what is the argument for calleding after the loop.


Are you asking why the following code is not used?

if (...condition...) { // Wrong...
lock.wait();
}

Firstly there is the problem of spurious wakeups. wait can exit at any
time, without reason. Therefore the loop is always necessary.

Even if there were no spurious wakeups, it's still usually wrong.
Typically you may have multiple threads waiting, only one of which can
proceed. Each thread will need to check the notify is for them. For
instance, two threads are waiting to take an element off a blocking
queue, if notifyAll is used both will wake but only one should grab the
new element.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
 
 
 
hiwa
Guest
Posts: n/a
 
      05-07-2006

puzzlecracker のメッセージ:

> The argument is that calling wait prior to loop me cause a thread never
> to be aweken (given that notify[all] has been called prior)
>
> How does waiting in the loop prevent that:
>
> while(a condition){
> obj.wait();
>
> }
> and notify has been called prior to wait, indefinite sleep is
> guaranteed.
>
> what is the argument for calleding after the loop.
>
> thanks

I don't understand your English.

 
Reply With Quote
 
Mitch
Guest
Posts: n/a
 
      05-08-2006
puzzlecracker wrote:
> The argument is that calling wait prior to loop me cause a thread never
> to be aweken (given that notify[all] has been called prior)
>
> How does waiting in the loop prevent that:
>
> while(a condition){
> obj.wait();
>
> }
> and notify has been called prior to wait, indefinite sleep is
> guaranteed.
>
> what is the argument for calleding after the loop.
>
> thanks
>


Do you mean that you are calling notify sometimes before the obj.wait()
and its loop are called? If this is the case, when you call notify you
could set a flag boolean notifyCalled = true and in the while loop you
could write while(a condition && !notifyCalled)

But as mentioned before, your English means it is difficult to guess at
what you mean.
 
Reply With Quote
 
Mitch
Guest
Posts: n/a
 
      05-08-2006
Mitch wrote:
> puzzlecracker wrote:
>> The argument is that calling wait prior to loop me cause a thread never
>> to be aweken (given that notify[all] has been called prior)
>>
>> How does waiting in the loop prevent that:
>>
>> while(a condition){
>> obj.wait();
>>
>> }
>> and notify has been called prior to wait, indefinite sleep is
>> guaranteed.
>>
>> what is the argument for calleding after the loop.
>> thanks
>>

>
> Do you mean that you are calling notify sometimes before the obj.wait()
> and its loop are called? If this is the case, when you call notify you
> could set a flag boolean notifyCalled = true and in the while loop you
> could write while(a condition && !notifyCalled)
>
> But as mentioned before, your English means it is difficult to guess at
> what you mean.


Actually you would probably write

if(!notifyCalled){
while(a condition){
obj.wait();
}
}

I wrote the previous in a general sense, only to realise that after
posting, it wouldn't actually work.
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      05-08-2006
On Mon, 08 May 2006 13:16:37 +0100, Mitch wrote:
> Actually you would probably write
>
> if(!notifyCalled){
> while(a condition){
> obj.wait();
> }
> }


No. There is already a "real" reason for the notification ("a
condition"), and it should be set by the notifying thread before
notify() is called, within the synchronized block:

synchronized (someObject) {
setCondition(true);
someObject.notify();
}

The waiting thread *must* test for that condition before calling
wait() in every case. This is sufficient to avoid problems caused by
calling notify() prior to wait():

synchronized (someObject) {
while (!getCondition()) { /* until the condition becomes true */
someObject.wait();
}
}

Adding an additional, artifical condition (notifyCalled) gains you
nothing and in fact only distracts from the real condition that is
being communicated between the threads. Understand too the distinction
between a condition stated in terms of the problem domain (e.g. "data
is ready"), and in terms of the implementation ("notify was called").
The latter of these indicates poor design IMO.

The loop is necessary in the case of spurious interrupts, but it also
lets multiple waiting threads contend on a single lock when
notifyAll() is used (and in this case, the condition should likely be
"cleared" by the first waiting thread that discovers the condition).

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
help with pix inside->outside + dmz->outside + inside->outside->dmz Jack Cisco 0 09-19-2007 01:57 AM
nat from outside to outside dt1649651@yahoo.com Cisco 1 08-21-2005 04:26 PM
Condition outside loop or separate loop for different condition? - Java 12 06-15-2005 08:50 AM
How to make "fork/wait" to WAIT longer? Huey C Programming 1 03-01-2004 02:01 PM



Advertisments