Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

calling wait outside of loop

Thread view: 
puzzlecracker - 07 May 2006 03:15 GMT
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
Thomas Hawtin - 07 May 2006 10:48 GMT
> 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)
[quoted text clipped - 9 lines]
>
>  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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

hiwa - 07 May 2006 11:07 GMT
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)
[quoted text clipped - 11 lines]
>
> thanks
I don't understand your English.
Mitch - 08 May 2006 13:13 GMT
> 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)
[quoted text clipped - 11 lines]
>
> 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.
Mitch - 08 May 2006 13:16 GMT
>> 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)
[quoted text clipped - 18 lines]
> 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.
Gordon Beaton - 08 May 2006 13:59 GMT
> Actually you would probably write
>
[quoted text clipped - 3 lines]
>     }
> }

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

Signature

[  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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.