服务器/客户端停止1个线程= 1个客户端 [英] Server/Client stop 1 thread = 1 clients
问题描述
协议:TCP
我有一台带有SocketServer的服务器,该服务器接受客户端并将套接字(由ss.accept()
返回)放置在数组中.
I have a server with a SocketServer that accepts clients and puts the socket (returned by ss.accept()
) in an array.
当准备好读取套接字(客户端发送的数据)时,我需要通知1个线程. 该线程会将请求分派给其他线程进行处理,因此该初始线程的操作确实非常简单快捷.
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.
有可能吗?
我真的想避免1个线程= 1个客户端,并让1个线程处理N个客户端.
I really want to avoid 1 thread = 1 client and have 1 thread deal with N clients.
推荐答案
我认为您可能想看看高度可扩展的基于NIO的服务器的体系结构"
另外,我认为此问题的答案将为您提供帮助.
I think you might want to look at the non-blocking IO aspects of Java NIO some of which are discussed in this article about the "Architecture of a Highly Scalable NIO-Based Server"
Additionally I think the answer to this question will help you.
通常,非阻塞IO使用回调-当数据可用时,将需要您的代码将处理程序注册为被回调".在这种情况下,处理程序会将消息或数据发布到您的单线程中-必须具有线程安全性才能处理并发的通知/回调.
Typically non-blocking IO makes use of callbacks - your code will be required to register a handler to be "called-back" when the data becomes available. In this case the handler would post a message or data to your single thread - which will have to be threadsafe to handle concurrent notifications/callbacks.
这篇关于服务器/客户端停止1个线程= 1个客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!