S
Slobodan C
I am looking for java.util.List implementation for event listeners
that merges several concepts:
1) Jakarta Commons Collection library has a
org.apache.commons.collections.FastArrayList which avoid the overhead
of synchronization using copy-on-write mode. The idea is that when
dominant ops are reads (such as event listener lists) then it is
cheaper to copy entire list before changing it to avoid
synchronization on reads.
2) Related to (1), when firing events using an iterator, iterator
object needs to keep traversing the old copy of a list if the list is
changed during event firings. Although FastArrayList claims to have
this behaviour, it does the opposite and throws the exception (see
Appendix A)
3) As has been discussed many times before, listeners must be
referenced using a weak reference. Would be nice if the list
implementation would take care of it internally and caller need not
worry about it.
My question is if such an implementation exists some place.
Sub question: If you think that (1) and (2) are overkill and
synchronization is simpler and as fast as copy-on-write, tell me why.
Appendix A:
From the Commons Collection 2.1 doc,
org.apache.commons.collections.FastArrayList:
public Iterator iterator()
Return an iterator over the elements in this list in proper
sequence.
IMPLEMENTATION NOTE - If the list is operating in fast mode, an
Iterator is returned, and a structural modification to the list is
made, then the Iterator will continue over the previous contents of
the list (at the time that the Iterator was created), rather than
failing due to concurrent modifications.
Test:
FastArrayList l = new FastArrayList();
l.setFast( false );
l.add("a");
l.add("b");
l.add("c");
l.setFast( true );
System.out.println("FAST = "+ l.getFast());
Iterator seq = l.iterator();
System.out.println("BEFORE "+ seq.next());
l.add("Z");
System.out.println("AFTER "+ seq.next());
FAST = true
BEFORE a
java.util.ConcurrentModificationException
at org.apache.commons.collections.FastArrayList$ListIter.checkMod(Unknown
Source)
at org.apache.commons.collections.FastArrayList$ListIter.next(Unknown
Source)
at brisi.main(brisi.java:22)
Exception in thread "main"
According to the doc I should not get the exception since the list is
in fast mode. Yet it is thrown and looking at the source it seems to
be a deliberate check, so no accident. I can only assume that the doc
hasn't been updated.
that merges several concepts:
1) Jakarta Commons Collection library has a
org.apache.commons.collections.FastArrayList which avoid the overhead
of synchronization using copy-on-write mode. The idea is that when
dominant ops are reads (such as event listener lists) then it is
cheaper to copy entire list before changing it to avoid
synchronization on reads.
2) Related to (1), when firing events using an iterator, iterator
object needs to keep traversing the old copy of a list if the list is
changed during event firings. Although FastArrayList claims to have
this behaviour, it does the opposite and throws the exception (see
Appendix A)
3) As has been discussed many times before, listeners must be
referenced using a weak reference. Would be nice if the list
implementation would take care of it internally and caller need not
worry about it.
My question is if such an implementation exists some place.
Sub question: If you think that (1) and (2) are overkill and
synchronization is simpler and as fast as copy-on-write, tell me why.
Appendix A:
From the Commons Collection 2.1 doc,
org.apache.commons.collections.FastArrayList:
public Iterator iterator()
Return an iterator over the elements in this list in proper
sequence.
IMPLEMENTATION NOTE - If the list is operating in fast mode, an
Iterator is returned, and a structural modification to the list is
made, then the Iterator will continue over the previous contents of
the list (at the time that the Iterator was created), rather than
failing due to concurrent modifications.
Test:
FastArrayList l = new FastArrayList();
l.setFast( false );
l.add("a");
l.add("b");
l.add("c");
l.setFast( true );
System.out.println("FAST = "+ l.getFast());
Iterator seq = l.iterator();
System.out.println("BEFORE "+ seq.next());
l.add("Z");
System.out.println("AFTER "+ seq.next());
FAST = true
BEFORE a
java.util.ConcurrentModificationException
at org.apache.commons.collections.FastArrayList$ListIter.checkMod(Unknown
Source)
at org.apache.commons.collections.FastArrayList$ListIter.next(Unknown
Source)
at brisi.main(brisi.java:22)
Exception in thread "main"
According to the doc I should not get the exception since the list is
in fast mode. Yet it is thrown and looking at the source it seems to
be a deliberate check, so no accident. I can only assume that the doc
hasn't been updated.