> -----Original Message-----
> From: Niclas Hedhman [mailto:[EMAIL PROTECTED]
> Sent: den 26 februari 2004 17:11
> To: Avalon Developers List
> Subject: Re: [RT] Notification pattern has interesting effects on IoC
>
>
> On Thursday 26 February 2004 23:36, Jonathan Hawkes wrote:
> > Isn't copy-on-write broken along with double-checked locking (under
> > the
> > 1.4- memory model)?
>
> I would be happy to see any references on this.
>
> My typical code looks;
>
> public void addMyListener( MyListener listener )
> {
> synchronized( m_Listeners )
> {
> ArrayList clone = (ArrayList) m_Listeners.clone();
> clone.add( listener );
> m_Listeners = clone;
> }
> }
>
> public void removeMyListener( MyListener listener )
> {
> synchronized( m_Listeners )
> {
> ArrayList clone = (ArrayList) m_Listeners.clone();
> clone.remove( listener );
> m_Listeners = clone;
> }
> }
>
> And I can't figure out how this could be faulty, even on
> exotic hardware.
But don't you have to synchronize on a lock for m_Listeners when
you traverse the list for sending notifications?
Otherwise you can get:
public void addMyListener( MyListener listener )
{
synchronized( m_Listeners )
{
ArrayList clone = (ArrayList) m_Listeners.clone();
m_Listeners = clone;
clone.add( listener );
}
}
Due to out-of-order-execution.
util.concurrent has a CopyOnWriteArrayList, BTW, so
there's no need to implement this yourself...
/LS
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]