1

Protocol: TCP

I have a server with a SocketServer that accepts clients and puts the socket (returned by ss.accept()) in an array.

I have 1 thread that needs to be notified when a socket is ready to be read (client sent data). This thread will dispatch the request to other threads for processing so what this initial thread does is really simple and fast.

Is it possible?

I really want to avoid 1 thread = 1 client and have 1 thread deal with N clients.

4

1 回答 1

3

我想你可能想看看Java NIO的非阻塞 IO 方面,其中一些在这篇关于“基于高度可扩展的 NIO 的服务器的架构”的
文章中进行了讨论。 此外,我认为这个问题的答案会对你有所帮助。

通常非阻塞 IO 使用回调——当数据可用时,您的代码将需要注册一个处理程序以“回调”。在这种情况下,处理程序会将消息或数据发布到您的单线程 - 这必须是线程安全的才能处理并发通知/回调。

于 2012-07-24T10:30:16.970 回答