简体   繁体   English

Java:对httpRequest使用ExecutorService

[英]Java : Using ExecutorService for httpRequests

I have a Web Application where i receive a lot(100K per hour) of http requests. 我有一个Web应用程序,在其中我收到很多(每小时100K)http请求。 I do not have any concerns regarding the Servlet container thread handling. 我对Servlet容器线程处理没有任何担心。

However, for each such web request, I would like to further multithread the process so that i can increase the response time of each request. 但是,对于每个这样的Web请求,我想进一步对该过程进行多线程处理,以便我可以增加每个请求的响应时间。

I would like to use ExecutorService , initialize newFixedThreadPool for every such request and use future object to stitch back the response from all concurrent threads(for each request) and finally submit the response back to the client. 我想使用ExecutorService,为每个此类请求初始化newFixedThreadPool,并使用future对象从所有并发线程(对于每个请求)中缝合响应,最后将响应提交回客户端。

The advantage i see with this approach is my response time is drastically reduced as multiple threads are acting upon the same request rather than a single thread. 我看到的这种方法的优点是,由于多个线程正在处理同一请求,而不是单个线程,因此我的响应时间大大减少了。

Please suggest if this is the right way? 请提出这是否正确的方法?

Please suggest, is this the right way? 请提出建议,这是正确的方法吗?

Your server is handling almost 30 requests per second on average. 您的服务器平均每秒处理近30个请求。 How long does each request take to complete? 每个请求需要多长时间才能完成? How much CPU time does each request use? 每个请求使用多少CPU时间? What percent of the available CPU is that? 那是可用CPU的百分之几? How much time does each request spent waiting for I/O, or waiting to talk to other services (eg, a database)? 每个请求在等待I / O或等待与其他服务(例如数据库)交谈时花费了多少时间? If there's significant compute time, what is the computation? 如果有大量的计算时间,那么该怎么算? Does it make sense to parallelize it? 并行化是否有意义?

Nobody can say whether multi-threading is "the right way" without knowing the answers to those questions. 没有人知道这些问题的答案,谁也无法说出多线程是否是“正确的方法”。


Edit: You say each request takes 400ms. 编辑:您说每个请求需要400毫秒。 OK, if they're coming in at a steady 100K per hour pace, that means that at any given instant, your server will be processing about eleven simultaneous requests. 好吧,如果它们以每小时100K的速度稳定增长,这意味着在任何给定的瞬间,您的服务器将处理大约11个同时请求。

That's useful information, but maybe not as useful as the other questions that I asked: How much CPU time does each request use? 这是有用的信息,但可能不如我问的其他问题有用:每个请求使用多少CPU时间? What percent of the available CPU is that? 那是可用CPU的百分之几? How much time does it spend waiting for I/O? 等待I / O需要花费多少时间? What percent of the available I/O bandwidth is that? 那是什么百分比的可用I / O带宽?

You say that each request is composed of several independent parts. 您说每个请求都由几个独立的部分组成。

Maybe it will shorten the overall response time if you change the code to handle each part in a separate thread, and maybe it won't. 如果您更改代码以在单独的线程中处理每个部分,则可能会缩短总体响应时间,但可能不会。 There's just not enough information here---information about the hardware platform, information about your application, information about the resources that it uses---to predict the outcome. 此处没有足够的信息来预测结果,这些信息是有关硬件平台的信息,有关您的应用程序的信息,有关其使用的资源的信息。

Have you tried it? 你试过了吗?

You make it sound like you already know how to make the change. 您听起来好像已经知道如何进行更改。 You make it sound like it won't be difficult. 您听起来好像并不困难。 Sometimes, doing an experiment is the easiest way to find out. 有时,进行实验是找出答案的最简单方法。

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

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