我正在尝试在java
中实现Web套接字服务器。服务器的代码是:-
ServerSocket serverSocket = new ServerSocket();
SocketAddress socketAddress = new InetSocketAddress(9000);
LOGGER.info("Listening on port :: 9000");
serverSocket.bind(socketAddress,5 );
while (true) {
Socket socket = serverSocket.accept();
Processor processor = Processor.getInstance(processorCounter++);
try {
processor.begin(socket);
} catch (AppException e) {
LOGGER.info(e.getMessage(), e);
}
}
我正在创建一个新线程Processor
,该线程接受socket
并开始在不关闭连接的情况下在其上发布response
。这种联系是长期而持久的。
当我用大约2000 requests
命中该服务器时,发现创建的数量不超过249 threads
。问题在于,为什么生成的数量不超过249 threads/processors
?
PS ::
requests
是从Google Chrome发送的
发出请求的Javascript
-
const func1=function(){
for(var i=1;i<=2000;i++){
var ws=new WebSocket("ws://localhost:9000");
ws.onopen=function(event){
ws.send("are you a teapot?! from client "+i);
};
ws.onmessage=function(event){
console.log("Server says : "+event.data);
};
ws.onerror=function(event){
console.log("error () -> "+JSON.stringify(event));
};
ws.onclose=function(event){
console.log("close () -> "+JSON.stringify(event));
};}
}