D
Daisy
I'm getting a java.util.NoSuchElementException at high loads using
java.util.concurrent.CopyOnWriteArraySet. I have one guess why and
would like to hear if it makes sense. Could this error occur because
two threads call the same instance?
For example, thread A executes i.hasNext() which returns true. Then
thread B runs and happens to start executing at i.next(). When thread
A runs again, it will execute the i.next() but B has already advanced
the iterator to the end of the list.
I'm using CopyOnWriteArraySet to avoid synchronizing. Do I have to
sync to avoid this issue?
Thanks for the opinions!
public class DistributionSet extends CopyOnWriteArraySet {
private Iterator i;
public void enqueue( Object message ) {
for ( i = this.iterator( ) ; i.hasNext( ) ; ) {
// .next() call is throwing an error - why? either:
// copy hasn't completed or .hasNext() has a different
count, or some listener was removed
EventListener listener = ( EventListener ) i.next( );
listener.eventObserved( message ) ;
}
}
public boolean add( EventListener consumer ) {
super.add( consumer );
return true;
}
}
java.util.concurrent.CopyOnWriteArraySet. I have one guess why and
would like to hear if it makes sense. Could this error occur because
two threads call the same instance?
For example, thread A executes i.hasNext() which returns true. Then
thread B runs and happens to start executing at i.next(). When thread
A runs again, it will execute the i.next() but B has already advanced
the iterator to the end of the list.
I'm using CopyOnWriteArraySet to avoid synchronizing. Do I have to
sync to avoid this issue?
Thanks for the opinions!
public class DistributionSet extends CopyOnWriteArraySet {
private Iterator i;
public void enqueue( Object message ) {
for ( i = this.iterator( ) ; i.hasNext( ) ; ) {
// .next() call is throwing an error - why? either:
// copy hasn't completed or .hasNext() has a different
count, or some listener was removed
EventListener listener = ( EventListener ) i.next( );
listener.eventObserved( message ) ;
}
}
public boolean add( EventListener consumer ) {
super.add( consumer );
return true;
}
}