Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

weird CopyOnWriteArraySet error

Thread view: 
Daisy - 28 Oct 2006 20:19 GMT
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;  
   
   }
}
hiwa - 29 Oct 2006 00:37 GMT
> Do I have to
> sync to avoid this issue?
I don't think so. But try anyway and post the result.
Your code may have bugs....
Patricia Shanahan - 29 Oct 2006 01:14 GMT
> I'm getting a java.util.NoSuchElementException at high loads using
> java.util.concurrent.CopyOnWriteArraySet.  I have one guess why and
[quoted text clipped - 34 lines]
>     }
> }

If enqueue can be called in multiple threads, then there is a risk of
NoSuchElementException. For example:

Suppose thread A calls enqueue, and enqueue assigns to i the Iterator
reference iterator() returned, and i.hasNext() returns true. Now A gets
interrupted, thread B starts running, and thread B calls enqueue.

Thread B assigns to i the Iterator reference that its iterator() call
returned. At that point, the thread A Iterator becomes unreachable.
Thread B completes its enqueue call, leaving i referencing an iterator
that returned false from i.hasNext().

Now thread A gets control back, but i references the Iterator that
thread B was using. The thread A next call fails.

If the uses of the shared Iterator are finely interleaved, as they could
be on a dual processor, you could get other problems such as not passing
a particular message to some listeners.

However, it is a problem you created, by choosing to make i an instance
field forcing multiple threads to share the iterator reference. Why is
that necessary?

If i were local, each enqueue activation would be able to remember its
own Iterator reference.

Patricia
Daisy - 29 Oct 2006 13:04 GMT
Thank you Patricia for the excellent response.

I originally had i as a local variable, but moved it to be an instance
variable to make the code faster.  I'll move it back being a local
variable.

Thanks

> > I'm getting a java.util.NoSuchElementException at high loads using
> > java.util.concurrent.CopyOnWriteArraySet.  I have one guess why and
[quoted text clipped - 62 lines]
>
> Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.