How to modify that class:
public class Event<T extends EventArgs> {
private final List<Listener<T>> listeners;
public Event() {
listeners = new ArrayList<>();
}
public void invoke(T args) {
for (Listener<T> l : listeners)
l.onEvent(args);
}
public void addEventListener(Listener<T> l) {
listeners.add(l);
}
public void removeEventListener(Listener<T> l) {
listeners.remove(l);
}
}
So addEventListener()
and removeEventListener()
will be postponed to execute after invoke()
ends (so I won't get ConcurrentModificationException
)?