tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Schmiedel <martin.schmie...@gmail.com>
Subject WebSockets Thread Safety question
Date Mon, 03 Jun 2013 07:45:29 GMT
I have recently had problems with a simple WebSocket sample I'm developing.

Tomcat Version is 7.0.39.

When I use the syntax from the samples in the onTextMessage() method, I get
ConcurrentModificationException if I have more than one client sending data
to the server at the same time:

            for(MyMessageInbound mmib: mmiList){
                CharBuffer buffer = CharBuffer.wrap(cb);
                mmib.myoutbound.writeTextMessage(buffer);
                mmib.myoutbound.flush();
            }


Changing it to the following works fine:

            for(int i = 0; i < mmib.size(); i++) {
                MyMessageInbound mmib = mmiList.get(i);
                CharBuffer buffer = CharBuffer.wrap(cb);
                mmib.myoutbound.writeTextMessage(buffer);
                mmib.myoutbound.flush();
            }

However, this approach is not as efficient as to use an Iterator, unless I
clone the mmiList Collection to iterate over it...


About the mmiList object, why isn't it a Threadsafe Collection?

What's the recommended approach for this?


Regards

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message