我应该在请求中创建executorService还是在Web应用程序中共享一个实例? [英] Should I create executorService within a request or share one instance across the webapp?
问题描述
我正在向基于Jersey的Web服务添加新的端点.支持端点的逻辑需要对另一个服务进行10到50次调用.这些调用是独立的并且可以并行化,因此我在考虑使用执行程序服务在多个线程之间分配工作.
I am adding a new endpoint to a Jersey-based web-service. The logic backing the endpoint needs to make between 10 to 50 calls to another service. The calls are independent and can be parallelized, so I was thinking of using executor service to distribute the work across multiple threads.
我想知道应该为每个请求实例化executorService还是在Web应用程序中共享一个executorService实例.在后一种情况下,我将如何确定它应运行的线程数?
I am wondering if I should instantiate an executorService for each request or should there be a shared executorService instance across the webapp. In the later case, how would I decide the number of threads it should run?
推荐答案
我想知道是否应该为每个实例化executorService. 请求还是应该有一个共享的executorService实例 网络应用程序.在后一种情况下,我将如何确定 应该运行的线程?
I am wondering if I should instantiate an executorService for each request or should there be a shared executorService instance across the web app. In the later case, how would I decide the number of threads it should run?
不.通常,您不应该为每个Web请求实例化executorService
(选项1),因为服务器很快就会用完内存,并且动态创建线程池非常昂贵(耗时).
No. In general, you should NOT instantiate the executorService
for each web request (option-1) because the server will soon run out of memory and also, thread pool creation on the fly is costly (time-consuming).
因此,您需要通过在服务器启动期间创建executorService
的共享实例(选项2)来进行启动&根据您的要求并进行性能测试来配置线程池大小.
So you need to go with the shared instance (option-2) of the executorService
by creating it during server startup & configure the thread pool size according to your requirements and by conducting the performance tests.
您可以在此处进行参考,以了解该线程池.
You can refer here to understand on the thread pools.
这篇关于我应该在请求中创建executorService还是在Web应用程序中共享一个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!