SocketChannels, Selectors, Iterators and ConcurrentModificationExceptions,oh my.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank van Schie

    #1

    SocketChannels, Selectors, Iterators and ConcurrentModificationExceptions,oh my.

    Hi there,

    I'm trying to make an instant messaging connection library that's rather
    sparse with Threads, due to it being meant for a transport for XMPP.
    Most libraries out there create a Thread for damn near everything that's
    going on, so I'm writing my own.

    I'm running into a bit of trouble with my Selector, which I periodically
    poll from the lone Thread I have so far. If I have just one
    SocketChannel registered, I have no problems, and the thing connects
    fine and receives all the input it should. If I have two connections
    open at the same time, then when I get my SelectionKey iterator and hit
    'next()', it blows up with a java.util.Concu rrentModificati onException.

    While the iterator can throw the Exception if there is even the chance
    of the iterator's underlying data being modified, I am confused by this
    not being a problem if I only have the single channel open. I have
    checked and verified that I am not accidentally starting two Threads
    (and even so, wouldn't the synchronization on the iterator be sufficient
    to satify it?)

    I'm not experienced at all with socket programming in general, and
    java.nio.* in particular, so I wonder if anyone here could shed some light?

    Some code from my manager Thread's run method:

    public void run()
    {
    [...]
    while(!stopped)
    {
    this.selector.s electNow();
    Iterator<Select ionKey> selectionKeyIte rator =
    selector.select edKeys().iterat or();
    synchronized(se lectionKeyItera tor)
    {
    SelectionKey selectionKey = null;
    while(selection KeyIterator.has Next())
    {
    selectionKey = selectionKeyIte rator.next(); // This goes BOOM.
    selectionKeyIte rator.remove();
    /**
    * Verify that the key is valid, get the socketchannel from the
    * key, get my connection object, have it read the data, send it
    * for processing by a 'listener' of sorts, etc...
    }
    }

    Thread.sleep(50 ); // Complete with try/catch, natch.
    }
    }

    --
    Frank
  • Frank van Schie

    #2
    Re: SocketChannels, Selectors, Iterators and ConcurrentModif icationExceptio ns,oh my.

    Frank van Schie wrote:[color=blue]
    > Hi there,
    >
    > I'm trying to make an instant messaging connection library that's rather
    > sparse with Threads, due to it being meant for a transport for XMPP.
    > Most libraries out there create a Thread for damn near everything that's
    > going on, so I'm writing my own.
    >
    > I'm running into a bit of trouble with my Selector, which I periodically
    > poll from the lone Thread I have so far. If I have just one
    > SocketChannel registered, I have no problems, and the thing connects
    > fine and receives all the input it should. If I have two connections
    > open at the same time, then when I get my SelectionKey iterator and hit
    > 'next()', it blows up with a java.util.Concu rrentModificati onException.[/color]

    [snip rest]

    Obviously, I might have better luck getting help in the
    comp.lang.java. help group, which I've just found, almost 10 hours after
    this post. With research skills like these, how come I haven't solved
    this yet??

    I'm reposting it there, since it is more appropriate. I'd cancel this
    message, but that wouldn't work well anyway. Apologies.
    --
    Frank

    Comment

    Working...