简体   繁体   English

异步处理J2EE

[英]Async processing J2EE

In Servlet 3.0 the concept of async processing was introduced. 在Servlet 3.0中引入了异步处理的概念。 So all the books says that eliminates the requirement one thread per request. 所以所有的书都说每个请求消除了一个线程的要求。 I have tested that and yeah, it actually works. 我测试了它,是的,它确实有效。 Now, I have a simple servlet where users initiate an HTTP request, in sync mode. 现在,我有一个简单的servlet,用户在同步模式下发起HTTP请求。 The thread is simply sleeps for 1 second and then replies to the client. 线程只是休眠1秒钟然后回复客户端。 When I do a load testing against this mode, the server can handle 4 request per second only. 当我针对此模式进行负载测试时,服务器每秒只能处理4个请求。 Now, I change the sync mode to async mode and create a new thread on request an release the original http thread back to pool. 现在,我将同步模式更改为异步模式,并根据请求创建新线程,将原始http线程释放回池。 Again, the new thread initiated sleeps for 1 second and replies back.That mode however, scales very good, it handles hundreds of requests per second. 同样,新线程启动休眠1秒钟并回复。但是,这种模式非常好,它每秒处理数百个请求。

Now, the question is, all the books and articles says the server has limited number of recourses so keeping one thread per request is not good. 现在,问题是,所有书籍和文章都说服务器的资源数量有限,因此每个请求保留一个线程并不好。 etc. In both scenarios, I have one thread per request, the main difference is in first one it is an http thread and in the second one it is my custom thread. 在这两种情况下,每个请求都有一个线程,主要区别在于,第一个是http线程,第二个是我的自定义线程。 Now the question is, is there anything special about the HTTP threads than my custom threads? 现在的问题是,HTTP线程有什么特别的,而不是我的自定义线程? After all, in both case we have the one thread per request, why the first one performs bad but the second one does not? 毕竟,在这两种情况下,我们每个请求都有一个线程,为什么第一个执行不好但第二个不执行? I have gone through many documents, books none explains that tricky detail. 我经历了很多文件,书籍都没有解释那些棘手的细节。 Can you suggest me something? 你能给我一些建议吗? Thanks 谢谢

AFAIK, there is no difference between HTTP threads and Asychronous Threads... AFAIK,HTTP线程和Asychronous线程之间没有区别......

You see a performance upgrade is because HTTP threads (mostly) are implemented as a fixed N-size pool of threads; 您看到性能升级是因为HTTP线程(大多数)是作为固定的N大小的线程池实现的; meaning that at last N server requests will be processed concurrently... any other requests will block (or rejected) until one or more threads become free. 这意味着最后将同时处理N个服务器请求...任何其他请求将阻塞(或拒绝),直到一个或多个线程变为空闲。

In the Asynchronous mode, those N threads are used and released very quickly because the hard work (creating the response object) is being done in another thread(s); 在异步模式下,这些N个线程被非常快速地使用和释放,因为艰苦的工作(创建响应对象)正在另一个线程中完成; allowing you to continue receiving more hit requests. 允许您继续接收更多命中请求。

Depending how you implement the background threads, you will see the performance upgrade. 根据您实现后台线程的方式,您将看到性能升级。 For example, if you also implement your threads as a fixed M-size pool, where M is bigger than N, you will see an increment of M - N requests in processing. 例如,如果您还将线程实现为固定的M大小池,其中M大于N,您将看到处理中M - N请求的增量。

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

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