Using CopyOnWriteArrayList for thread safety.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@966818 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James W. Carman 2010-07-22 19:31:48 +00:00
parent 973bdd708b
commit 6967edc80c
1 changed files with 5 additions and 4 deletions

View File

@ -24,6 +24,7 @@
import java.lang.reflect.Proxy;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* An EventListenerSupport object can be used to manage a list of event listeners of a particular type.
@ -48,7 +49,7 @@
*/
public class EventListenerSupport<L>
{
private final List<L> listeners = new LinkedList<L>();
private final List<L> listeners = new CopyOnWriteArrayList<L>();
private final L proxy;
/**
@ -110,7 +111,7 @@ public L fire()
public void addListener(L listener)
{
Validate.notNull(listener, "Listener object cannot be null.");
listeners.add(0, listener);
listeners.add(listener);
}
/**
@ -141,9 +142,9 @@ private class ProxyInvocationHandler implements InvocationHandler
{
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
for (int i = listeners.size() - 1; i >= 0; --i)
for (L listener : listeners)
{
method.invoke(listeners.get(i), args);
method.invoke(listener, args);
}
return null;
}