Java 中多线程的替代方案

发布于 2024-11-03 12:01:18 字数 231 浏览 1 评论 0 原文

有一个问题困扰了我一段时间。 例如,我有一个多线程服务器,当它收到一个请求时,它会将这个请求传递给一个处理程序,这个处理程序将处理这个请求。我们使服务器成为多线程的原因之一是: 如果不是多线程,当服务器处理这个请求的时候,在有意义的时间内, 另一个请求到来,那么该请求将被丢弃,因为服务器现在不可用。

所以我想知道多线程服务器是否有替代方案,例如我们可以为非多线程服务器创建一个队列?当它完成一个请求后可以从队列中获取另一个请求。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断桥再见 2024-11-10 12:01:18

是的,您可以拥有一个基于事件的服务器。此功能由 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.

定格我的天空 2024-11-10 12:01:18

是的,你可以。您是否考虑过类似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.

゛时过境迁 2024-11-10 12:01:18

您仍然可以使用带有多线程服务器的单线程引擎。

考虑以下框架 - 如果您有一个正在运行的引擎,它可以是完全单线程的,只需按照收到的顺序处理请求。这允许您在业务逻辑中使用非线程安全组件,并且您已经成功地将网络层与业务逻辑层分开!这是一个双赢的局面。

class Engine implements Runnable {

    private final Object requestLock = new Object();
    private List<Request> requests = new LinkedList<Request>();
    private boolean running = true;

    private Request nextRequest() {
        synchronized(requestLock) { return requests.poll(); }
    }

    /**
     * The engine is single threaded. It doesn't care about server connections
     */
    public void run() {
        while(running) {
            Request request = nextRequest();
            // handle your request as normal
            // also consider making a mechanism to send Responses
        }
    }
}

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.

class Engine implements Runnable {

    private final Object requestLock = new Object();
    private List<Request> requests = new LinkedList<Request>();
    private boolean running = true;

    private Request nextRequest() {
        synchronized(requestLock) { return requests.poll(); }
    }

    /**
     * The engine is single threaded. It doesn't care about server connections
     */
    public void run() {
        while(running) {
            Request request = nextRequest();
            // handle your request as normal
            // also consider making a mechanism to send Responses
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文