在Netty中增加工作线程数或创建自己的线程池是否更好? [英] Is it better to increase the number of worker threads or create my own thread pool in Netty?

查看:1251
本文介绍了在Netty中增加工作线程数或创建自己的线程池是否更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我们的Netty服务器(4.1.32)响应http调用.让我们进一步假设它必须执行某些阻止操作才能应答传入的请求,例如,它必须执行传出调用(在此处使用其他库)以加载外部数据.

Let's assume our Netty server (4.1.32) responds http calls. Let's further assume it must perform certain blocking actions before incoming requests can be answered, for example, it must perform an outgoing call (using a different library here) to load external data.

具有持久连接的NioEventLoopGroup的线程数

如果我的messageReceived方法阻塞了某件事或需要很长时间才能完成?

What happens if my messageReceived method blocks on something or takes a long time to complete?

@Maksym回应的

which @Maksym responds with

您应该避免在处理程序中使用线程阻塞操作.

You should avoid using thread blocking actions in your handlers.

很明显,只有有限数量的工作线程.因此,有效地阻塞所有可用的工作线程意味着Netty将对任何其他请求进行排队,直到工作线程可用为止.另一方面,按照建议将阻塞操作移到我自己的线程上,会对线程切换产生性能影响,而可用硬件会阻塞的是我自己的线程池.恕我直言,使用我自己的线程池只会增加另一层复杂性,但不会提高性能.相反,我宁愿增加工作线程的最大数量,并让Netty负责对请求进行排队.

It's clear there is only a limited number of worker threads. So blocking all available worker threads effectively means Netty will queue any further requests, until worker threads become available. On the other hand, moving blocking actions onto my own threads as suggested, will have performance impact for thread switching, and what will in turn be blocked by the available hardware is my own thread pool. IMHO using my own thread pool only adds another layer of complexity, but does not enhance performance. Instead, I'd rather increase the maximum number of worker threads and have Netty do the job of queuing requests.

这里的建议做法是什么?

What is the suggested practice here?

推荐答案

您应该使用自己的线程池,即句点. Netty工作者线程在许多连接中共享,如果您阻止处理一个请求,它将损害其他请求.该设计假定您的代码快速返回,如果您需要阻止,则必须为此使用单独的线程.如果您需要执行非常昂贵的计算,在另一个线程中执行并返回,则同样适用.

You should use your own thread pool, period. The Netty worker threads are shared among many connections, if you block to handle one request it will hurt the others. The design assumes that your code returns quickly, if you need to block you must use a separate thread for that. The same applies if you need to perform a really expensive calculation, do it in another thread and return.

如果您已完成任何Swing编程,则可以将其与GUI线程进行比较.如果您阻止GUI线程,则用户界面将挂起并变得无响应.不适合用户界面,也不适合高性能网络应用程序!

If you have done any Swing programming you can compare it to the GUI thread. If you block the GUI thread the user interface hangs and becomes unresponsive. Not good for a user interface and not for a high-performance networking application!

非常清楚,对于经典IO,通常将一个线程分配给一个请求,并且如果该请求阻塞的不是世界末日,则如果线程池足够大,则其他线程可以处理其他请求.使用NIO,您不会为您的请求获得线程,而是由处理许多事件的IO处理事件的线程调用该线程,并且应该尽快执行该操作,以便该线程可以移至另一个线程.要求.阻塞确实对服务器不利.

just to be super clear, with classic IO one thread is typically assigned to a request and if the request blocks that is not the end of the world, if the thread pool is large enough other threads can process the other requests. With NIO you don't get a thread for your request, you get called by a thread that handles IO for many requests to process an event and you are supposed to do that as quickly as possible so that the thread can move on to the other requests. Blocking is really bad for the server.

这篇关于在Netty中增加工作线程数或创建自己的线程池是否更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆