Search code examples
javaniosocketchannel

SocketChannel.write() in a single thread processing multiple clients


my application has a queue with " outgoing network packets" (A POJO with a ByteBuffer and a SocketChannel) inside, consumed by a single thread that writes the data to the SocketChannel.

I do this to guarantee that every client that should receive packets, gets its turn. This means that SocketChannel.write() writes sequentially to multiple clients (= 1 at a time).

Can anyone tell me what could go wrong working like this? The SocketChannels are created from a ServerSocketChannel, so they're blocking.

I fear that the write() operation could block for 1 client, making the other clients wait...


Solution

  • The write() operation can indeed block in blocking mode. If you want fairness and single threading you will have to use non-blocking mode.