简体   繁体   English

每个连接的线程数与Java中所有连接的一个线程数

[英]Thread per connection vs one thread for all connections in java

I have two different types of server and clients working at the moment and i am trying to decide which one would be better for an MMO server or at least a small MMO like server with at least 100 players at a time. 目前,我有两种不同类型的服务器和客户端,我正在尝试确定哪种服务器更适合MMO服务器,或者至少适合像小型MMO这样的服务器,且每次服务器至少有100名玩家。

my first server is using a thread per connection model and sending objects over the socket using ObjectOutputStream. 我的第一台服务器使用每个连接模型一个线程,并使用ObjectOutputStream通过套接字发送对象。

my second server is using java nio to use only one thread for all the connections and using select to loop through them. 我的第二台服务器使用java nio对所有连接仅使用一个线程,并使用select遍历它们。 this server is also using ObjectOutputStream to send data 该服务器还使用ObjectOutputStream发送数据

for my question, what would be a better approach to an MMO server and if the single thread model is better how would sending an object over the socket channel be affected, would it not be read all the way and not get the full object? 对于我的问题,什么是对MMO服务器更好的方法?如果单线程模型更好,则如何影响通过套接字通道发送对象,将不会一直读取并且不会获得完整对象?

for each object being sent over it just contains for example an int and 2 floats for sending position and player id. 对于每个要发送的对象,它仅包含一个int和2个浮点数,用于发送位置和玩家ID。

I will relate this question to why MMO use UDP over TCP. 我将把这个问题与为什么MMO在TCP上使用UDP有关。 The reason being that UDP promises fast delivery whereas TCP promises guaranteed delivery. 原因是UDP保证了快速传递,而TCP保证了传递。

A similar analogy can be applied to a single-threaded vs a multi-threaded model.Regardless of which you choose, your overall CPU cycles remain the same ie the server can process only so much information per second. 可以对单线程模型与多线程模型进行类似的模拟。无论选择哪种模式,您的总体CPU周期都保持不变,即服务器每秒只能处理这么多的信息。 Lets see what happens in each of these scenarios 让我们看看在每种情况下会发生什么

1. Single-Threaded Model : In this, your own implementation or the underlying library will end up creating a pipeline where the requests start queuing. 1. 单线程模型 :在这种情况下,您自己的实现或基础库最终将在请求开始排队的地方创建管道。 If you are at min load, the queue will remain virtually empty and execution will be real-time, however a lot of CPU may be wasted. 如果处于最小负载,队列将几乎保持为空,并且执行将是实时的,但是可能会浪费大量CPU。 At max load, there will be a long queue-up and execution will have latency with increasing load, however delivery will be guaranteed and CPU utilization will be optimum. 在最大负载下,将有很长的排队等待时间,并且随着负载的增加,执行将有延迟,但是将保证交付,并且CPU利用率将达到最佳。 Typically a slow client will slow everybody else down. 通常,速度较慢的客户会拖慢其他所有人的速度。

  1. Multi-Threaded Model : In this, depending on how your own implementation or the underlying library implements mutli-threading, parallel execution of requests will start happening. 多线程模型 :在这种情况下,根据您自己的实现或基础库如何实现多线程,将开始并行执行请求。 The catch to MT is that it's easy to get fooled. MT的缺点是容易上当。 For example, java.util.concurrent.ThreadPoolExecutor doesnt actually do any parallel processing unless you set the queue size to a low value. 例如,除非将队列大小设置为较小的值,否则java.util.concurrent.ThreadPoolExecutor实际上不会进行任何并行处理。 Once parallel processing starts happening, at min load, your execution will be superfast and CPU utilization will be optimum and game performance will be great. 一旦开始并行处理,在最小负载下,您的执行速度将超快,CPU利用率将达到最佳,游戏性能也将得到提升。 However, at max load your RAM usage will be high and CPU utilization will still be optimum. 但是,在最大负载下,您的RAM使用率将很高,而CPU使用率仍将是最佳状态。 Typically you'll need to put thread interrupts to avoid a slow client hogging all the threads, which will mean glitchy performance for the slow client. 通常,您需要放置线程中断,以避免慢速客户端占用所有线程,这将意味着慢速客户端出现故障。 Additionally as you start exhausting your thread pool and resources, threads will either get queued or just get dropped leading to glitchy performance. 此外,当您开始耗尽线程池和资源时,线程将进入队列或被丢弃,从而导致性能故障。

In gaming, performance matters more over stability, hence there is no question that you should use MT wherever you can, however tuning your thread parameters to compliment your server resources will decide whether its a boon or a complete bane 在游戏中,性能比稳定性更重要,因此,毫无疑问,您应该尽可能使用MT,但是通过调整线程参数来补充服务器资源将决定它是好事还是坏事

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM