TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.36k stars 2.57k forks source link

multi threading safety #1328

Closed trendspike closed 11 months ago

trendspike commented 1 year ago

I am using websocket. It is simple and powerful. The websocket server application has two threads which broadcast to clients. Are they thread safe? If no, how can I make sure the two threads can broadcast?

Currently I use lock. when one thread is broadcasting, I turn on the lock. Is the solution ok?

I am not a professional Java program. I appreciate your kind help.

lakeoffaith commented 1 year ago

you can turn list to ConcurrentLinkedQueue, then just call poll() to get item.
then server.broadcast(Collections.singletonList(item))

PhilipRoman commented 1 year ago

Broadcasting from multiple threads is fine