Home > Archive > Java Help > May 2006 > Re: SocketChannels, Selectors, Iterators and ConcurrentModificationExceptions, oh my.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: SocketChannels, Selectors, Iterators and ConcurrentModificationExceptions, oh my.
|
|
| Matt Humphrey 2006-05-19, 10:02 pm |
|
"Frank van Schie" <frankNOSPAM@email.it> wrote in message
news:3ImdnVbOE6mHs_PZnZ2dnUVZ8qadnZ2d@ca
sema.nl...
> [Note: Reposted my question from comp.lang.java. This group is more
> appropriate, not to mention more active.]
>
> 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.ConcurrentModificationException.
>
> While the iterator can throw the Exception if there is even the chance of
> the iterator's underlying data being modified, I am 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?)
The synchronized statement is faulty in your code because each thread is
synchronizing on a different object. The javadoc for Selector indicates
that the key set is not thread safe. Are you sure only one thread is
accessing the selector?A remove in one thread will likely cause the
ConcurrentModificationException in the other.
I othrewise don't see anything wrong with the iterator, unless the code
section commented out also adds to or removes from the
selector.selectedKeys().
Cheers,
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
|
|
|
|
|