java服务器:为每个收到的数据包启动一个新线程?

时间:2022-08-20 23:57:42

I am currently learning how to program a tcp server in java. I am creating a new thread for new each client connection (multithreaded server).

我目前正在学习如何在java中编写tcp服务器。我正在为新的每个客户端连接(多线程服务器)创建一个新线程。

After the client connects to the server, I want to send data from the client to the server. On serverside the server is in a while loop reading data from the sockets inputstream. I want to ask now: Should I create a new thread for each received packet?

客户端连接到服务器后,我想将数据从客户端发送到服务器。在服务器端,服务器处于while循环中,从套接字输入流中读取数据。我现在想问:我应该为每个收到的数据包创建一个新线程吗?

For example the client sends the string "Hello". Should the server handle this packet in a new thread so it can continue reading, because I think if I don't create a new thread, server doesn't go back on reading and if client sends another packet after the "Hello" packet for example "Bye", the server might not read it because it could still be busy handling the "Hello" packet.

例如,客户端发送字符串“Hello”。服务器是否应该在新线程中处理此数据包以便它可以继续读取,因为我认为如果我不创建新线程,服务器不会返回读取,如果客户端在“Hello”数据包之后发送另一个数据包示例“再见”,服务器可能无法读取它,因为它仍然可能忙于处理“Hello”数据包。

To be clear I am not talking about serversockets accept method, I am talking about the data from sockets inputstream.

要明确我不是在谈论serversockets接受方法,我在讨论来自socket输入流的数据。

Please enlighten me about this.

请赐教我这个。

3 个解决方案

#1


3  

If your packet handle task is not very heavy (probably it is not), you shouldn't. Thread will handle a message and come back to receiving new messages. No data will be lost - if nobody reads from inputstream, data transfer just holds (after all buffers filled). Just keep one thread per client, it is enough in most cases.

如果你的数据包处理任务不是很重(可能不是),你就不应该这么做。线程将处理消息并返回接收新消息。没有数据会丢失 - 如果没有人从输入流中读取,数据传输就会保持(在所有缓冲区填满之后)。只需为每个客户端保留一个线程,在大多数情况下就足够了。

#2


2  

Yes you must delegate to another thread in order to have a responsive server. No, don't create a thread per request (unless the client load is trivial) but use a thread pool instead. Example skeleton code (of course you will have to break out of the loop somehow):

是的,您必须委托另一个线程才能拥有响应式服务器。不,不要为每个请求创建一个线程(除非客户端加载很简单),而是使用线程池。示例框架代码(当然,你必须以某种方式突破循环):

private static final ExecutorService threadPool = Executors.newCachedThreadPool();  

while(true){  

  final Socket clientConnection = serverSocket.accept();  
  threadPool.execute(new Runnable(){  
            public void run(){  
                  processConnection(clientConnection);  
            });               

}  

Should I create a new thread for each received packet?

我应该为每个收到的数据包创建一个新线程吗?

No. Each thread will process its own connection (which receives the client packets) so connecting clients will be serviced faster since the main listener will continue accepting connections

不会。每个线程都会处理自己的连接(接收客户端数据包),因此连接客户端的服务速度会更快,因为主侦听器将继续接受连接

#3


0  

One thread per each client connection is fine for a small application. However, one thread per each client request / packet is not a good approach at all. Just make sure your server can communicate to the client in time.

每个客户端连接一个线程适用于小型应用程序。但是,每个客户端请求/数据包一个线程根本不是一个好方法。只需确保您的服务器能够及时与客户端通信。

Please note that the number of threads you create is no way unlimited, it depends on the OS you are working with. Real applications use thread pools for handling such cases.

请注意,您创建的线程数量无限制,这取决于您使用的操作系统。真实应用程序使用线程池来处理此类情况。

#1


3  

If your packet handle task is not very heavy (probably it is not), you shouldn't. Thread will handle a message and come back to receiving new messages. No data will be lost - if nobody reads from inputstream, data transfer just holds (after all buffers filled). Just keep one thread per client, it is enough in most cases.

如果你的数据包处理任务不是很重(可能不是),你就不应该这么做。线程将处理消息并返回接收新消息。没有数据会丢失 - 如果没有人从输入流中读取,数据传输就会保持(在所有缓冲区填满之后)。只需为每个客户端保留一个线程,在大多数情况下就足够了。

#2


2  

Yes you must delegate to another thread in order to have a responsive server. No, don't create a thread per request (unless the client load is trivial) but use a thread pool instead. Example skeleton code (of course you will have to break out of the loop somehow):

是的,您必须委托另一个线程才能拥有响应式服务器。不,不要为每个请求创建一个线程(除非客户端加载很简单),而是使用线程池。示例框架代码(当然,你必须以某种方式突破循环):

private static final ExecutorService threadPool = Executors.newCachedThreadPool();  

while(true){  

  final Socket clientConnection = serverSocket.accept();  
  threadPool.execute(new Runnable(){  
            public void run(){  
                  processConnection(clientConnection);  
            });               

}  

Should I create a new thread for each received packet?

我应该为每个收到的数据包创建一个新线程吗?

No. Each thread will process its own connection (which receives the client packets) so connecting clients will be serviced faster since the main listener will continue accepting connections

不会。每个线程都会处理自己的连接(接收客户端数据包),因此连接客户端的服务速度会更快,因为主侦听器将继续接受连接

#3


0  

One thread per each client connection is fine for a small application. However, one thread per each client request / packet is not a good approach at all. Just make sure your server can communicate to the client in time.

每个客户端连接一个线程适用于小型应用程序。但是,每个客户端请求/数据包一个线程根本不是一个好方法。只需确保您的服务器能够及时与客户端通信。

Please note that the number of threads you create is no way unlimited, it depends on the OS you are working with. Real applications use thread pools for handling such cases.

请注意,您创建的线程数量无限制,这取决于您使用的操作系统。真实应用程序使用线程池来处理此类情况。