Re: notify / notifyAll misunderstanding
From: VisionSet (spam_at_ntlworld.com)
Date: 07/13/04
- Next message: Chris Smith: "Re: notify / notifyAll misunderstanding"
- Previous message: Drew Volpe: "Re: CMS Written In JSP/Java"
- In reply to: Filip Larsen: "Re: notify / notifyAll misunderstanding"
- Next in thread: VisionSet: "Re: notify / notifyAll misunderstanding"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 13 Jul 2004 18:01:09 GMT
"Filip Larsen" <filip.larsen@nospam.dk> wrote in message
news:cd13nt$1928$1@news.cybercity.dk...
> Mike wrote
>
> > I have a record locking system that I have condensed to the simple
> example
> > below.
>
> > With notifyAll() I always get:
> >
> > locked 5
> > locked 6
> > unlocked 5
> > locked 5
>
> This looks like the correct behaviour because you have two threads
> trying to lock 5, and they both get turns.
No, I don't see it that way.
The 1st 5 thread will lock because 5 is not locked.
But when that is unlocked notifyAll will wake all threads but ONLY ONE will
aquire the lock on the Set object, this is arbitrary and sinece there are 7
lots of threads waiting to lock 6 versus 1 thread trying to lock 5 then it
is likely the 6 thread will win. Since notifyAll is only called once in this
example I don't see that the waiting threads get another chance to aquire
the lock.
>
> > notify() on the other hand works as expected ie:
> >
> > locked 5
> > locked 6
> > unlocked 5
>
> This does not look correct because when you use notify only one
> (arbitrary choosen) thread that wait on the lock gets notified and the
> second "lock 5" thread will therefore (probably) never be notified to
> discover that 5 indeed is available for locking again.
Yes I understand this, but for the same reason this should be the case for
notifyAll(), since although all waiting threads are woken (I know they
weren't sleeping) ONLY ONE will run and which one is arbitrary.
>
> As a rule of thumb you should only use notify when the condition you
> wait for is implied by the lock itself. If the wait is inside a while
> loop you probably need to use notifyAll. In your case you could for
> instance, with some modification to the code of course, wait for a
> separate object for each integer, in which case you *can* use notify
> because all threads that wait on the "5-object" is then in fact trying
> to lock 5.
Thanks that is useful, I'll give it some thought.
But my main reason for the post is to understand the notifyAll quandry,
which I still don't :-(
-- Mike W
- Next message: Chris Smith: "Re: notify / notifyAll misunderstanding"
- Previous message: Drew Volpe: "Re: CMS Written In JSP/Java"
- In reply to: Filip Larsen: "Re: notify / notifyAll misunderstanding"
- Next in thread: VisionSet: "Re: notify / notifyAll misunderstanding"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|