有一个问题困扰了我一段时间。
例如,我有一个多线程服务器,当它收到一个请求时,它会将这个请求传递给一个处理程序,这个处理程序将处理这个请求。我们使服务器成为多线程的原因之一是:
如果不是多线程,当服务器处理这个请求的时候,在有意义的时间内,
另一个请求到来,那么该请求将被丢弃,因为服务器现在不可用。
所以我想知道多线程服务器是否有替代方案,例如我们可以为非多线程服务器创建一个队列?当它完成一个请求后可以从队列中获取另一个请求。
I have a question bother me a while.
For example,I have a multithreaded server, when it receives a request, it pass this request to a handler, this handler will process this request. One reason we make server multithreaded is:
if it is not multithreaded, when the server processing this request, during the meaning time,
another request coming, then this request will be drop, because the server is not available now.
So I wonder if there is an alternative of multithreaded server, for example, we can create a queue for non-multithreading server? when it can fetch another request from the queue once it finish one.
发布评论
评论(3)
是的,您可以拥有一个基于事件的服务器。此功能由
java.nio
包提供,不过您可以使用 netty< 等框架/a> 而不是从头开始。但是,请注意,虽然这曾经被认为是获得更好性能的一种方法,但实际上它似乎是一个常规的多线程服务器 在当今的硬件和操作系统中提供更好的性能。
Yes, you can have an event-based server. This capability is offered by the
java.nio
package, though you could use a framework like netty rather than do it from scratch.However, note that while this used to be considered a way to get better performance, it seems like a regular multithreaded server actually offers better performances with today's hardware and operating systems.
是的,你可以。您是否考虑过类似SEDA技术(即事件驱动技术)?您可能还想研究 Netty 库。当涉及到使用 NIO 时,它会为您完成大部分工作。
Yes you can. Have you considered SEDA-like techniques (i.e. event-driven techniques)? You may want to investigate the Netty library too. It does most of the job for you when it comes to using NIO.
您仍然可以使用带有多线程服务器的单线程引擎。
考虑以下框架 - 如果您有一个正在运行的引擎,它可以是完全单线程的,只需按照收到的顺序处理请求。这允许您在业务逻辑中使用非线程安全组件,并且您已经成功地将网络层与业务逻辑层分开!这是一个双赢的局面。
You can still have a single threaded engine with a multi-threaded server.
consider the following skeleton - if you have an Engine that runs, it can be completely single threaded, just handing requests in the order they're received. This allows you to use non-thread-safe components in the business logic, and you've managed to separate your networking layer from your business logic layer! It's a win-win scenario.