cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dominique Devienne <DDevie...@lgc.com>
Subject RE: [OT] Build Time
Date Mon, 18 Aug 2003 14:33:42 GMT
Very clever! Does that work with removing a listener as well? I mean, what
about the unlikely event that a listener decides to remove itself from a
separate thread while an event is being fired from another thread, and thus
the list of listener traversed? Assume the listener being removed as not
been fired yet. I'd need to synchronize both removeListener and fireEvent,
no??? Just curious. I'm no threading expert for sure ;-) --DD

> -----Original Message-----
> From: Sylvain Wallez [mailto:sylvain.wallez@anyware-tech.com]
> Sent: Monday, August 18, 2003 3:18 AM
> To: dev@cocoon.apache.org
> Cc: dev@ant.apache.org
> Subject: Re: [OT] Build Time
> 
> To solve this problem, you may consider the fact that listeners are more
> often fired than changed, and therefore use a different pattern, which
> does the cloning and synchronisation when the listener list has to be
> changed, and not when it has to be traversed.
> 
> private List listenerers;
> 
> public void fireEvent(Event ev) {
>   Iterator iter = this.listeners.iterator();
>   while(iter.hasNext()) {
>     ((EventListener)iter.next()).fireEvent(ev);
>   }
> }
> 
> public void synchronized addListener(EventListener evl) {
>   // Make a local copy on which we will act : it avoids
>   // ConcurrentModificationException if someone is executing fireEvent()
>   List newListeners = this.listeners.clone();
>   newListeners.add(evl);
>   // and replace the listener list
>   this.listeners = newListeners;
> }
> 
> Sylvain

Mime
View raw message