Re: WebSockets Thread Safety question

2013-07-11 Thread Darryl Miles

Martin Gainty wrote:

use java.util.Collections.synchronizedList


or even better use a ConcurrentLinkedQueue class (with 
offer()/peek()/poll() APIs), as I suspect you never need to access the 
middle elements in the ordered list directly, so why carry this extra 
java.util.List API baggage/bloat in respect of the implementation chosen 
for efficient concurrency.


You can gain performance from being able to enqueue at one end and 
dequeue from the other end efficiently and as concurrently as possible.


Useful for handing off data to a shared I/O worker thread but be careful 
to consider flow control and back pressure where the enqueuing code will 
be forced to block or generate an error when there are is already too 
many bytes of data in the queue.


Adding in that kind of requirement makes things more tricky than 
ConcurrentLinkedQueue by itself can provide.



Darryl


-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



WebSockets Thread Safety question

2013-06-03 Thread Martin Schmiedel
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


Re: WebSockets Thread Safety question

2013-06-03 Thread Mark Thomas
On 03/06/2013 08:45, Martin Schmiedel wrote:
 About the mmiList object, why isn't it a Threadsafe Collection?

Where / how is that object declared?

Mark


-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: WebSockets Thread Safety question

2013-06-03 Thread chris derham
 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...

Can you explain where is the in-efficiency?

Thanks

Chris

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



RE: WebSockets Thread Safety question

2013-06-03 Thread Martin Gainty
/java/utiljavap Collections | grep synchronized
public static java.util.Collection synchronizedCollection(java.util.Collecti
on);
static java.util.Collection synchronizedCollection(java.util.Collection, jav
a.lang.Object);
public static java.util.Set synchronizedSet(java.util.Set);
static java.util.Set synchronizedSet(java.util.Set, java.lang.Object);
public static java.util.SortedSet synchronizedSortedSet(java.util.SortedSet)
;
public static java.util.List synchronizedList(java.util.List);
static java.util.List synchronizedList(java.util.List, java.lang.Object);
public static java.util.Map synchronizedMap(java.util.Map);
public static java.util.SortedMap synchronizedSortedMap(java.util.SortedMap)
;

use java.util.Collections.synchronizedList 

Martin 
__ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger 
sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung 
oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem 
Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. 
Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung 
fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le 
destinataire prévu, nous te demandons avec bonté que pour satisfaire informez 
l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est 
interdite. Ce message sert à l'information seulement et n'aura pas n'importe 
quel effet légalement obligatoire. Étant donné que les email peuvent facilement 
être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité 
pour le contenu fourni.


 From: ch...@derham.me.uk
 Date: Mon, 3 Jun 2013 07:42:01 -0300
 Subject: Re: WebSockets Thread Safety question
 To: users@tomcat.apache.org
 
  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...
 
 Can you explain where is the in-efficiency?
 
 Thanks
 
 Chris
 
 -
 To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
 For additional commands, e-mail: users-h...@tomcat.apache.org
 
  

Re: WebSockets Thread Safety question

2013-06-03 Thread Christopher Schultz
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

Chris,

On 6/3/13 6:42 AM, chris derham wrote:
 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...
 
 Can you explain where is the in-efficiency?

+1 -- the compiler generates pretty much identical bytecode in either
case.

I suppose having less actual code is a nice thing; I tend to agree
with that sentiment.

What I'd like to know is why there is a difference /at all/ in the two
code samples shown above.

- -chris
-BEGIN PGP SIGNATURE-
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJRrPPTAAoJEBzwKT+lPKRYkEsP/AiTKqT+ZM2uH9tmi2Ktcm2F
yjtSNQHW/iCp3aGZH+GsQf82FulpZh81j/fRAKJNf/5EWX8LQEdXZt7+r3OvlVs/
t0gF0CWgmyyF0blHu0L8/0uE4n1j+9fNpNdSKveQdfnXNoJ+3OAUd9FtkJfRz3cv
/VnIbztxmTdVtpFXRLCNLHSDd13zHKK8+KW/jE7W0aoQlxDFcnozdOniDZgvoIgv
r5YwgeZTdNLDf703DfspjjWkqc4rp1LVKjBAUcIbIIvqXMlNiXWrTWrMXHatcOQY
6pNJxmWXB05IOCzK39U2VX7v9vkbrh/8tpbNgM15MRoJlwm2sGwHHRCNWrgvS9Pj
YNVWaz/gED4GKVzs2ky+wYu1OQwVTOW3IJPktIORuf8w6RdPC9tHbzE7JdVBGabj
2vJ6t6xqHiEAw0deQPUvVZqvOWRELR+Zs1+++ESPswnl2Dn50QobN1JRVqmEEtVn
ZTxQE8onwCxv92yv4R5Yy/NtrHD8g02kMm12YqlFlT73fSGN+GR+uq0fgN8nYv1A
QIbVpZ4essX7LrhQ8HvRaiFX0RjwLiUHVlPDOagnHO4KFp+ttQK6DL19444Jv9Y/
gXCe1eFEexzF2MBdeNV+j3HjQCmCVJkGiCGoEXAwEKli5orNl8+WNts7TybAFIgg
n2HAWhN0WaU+/G8pEc3r
=DGKf
-END PGP SIGNATURE-

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org