我应该在请求中创建 executorService 还是在整个 webapp 中共享一个实例?

Should I create executorService within a request or share one instance across the webapp?

我正在向基于 Jersey 的 Web 服务添加一个新端点。支持端点的逻辑需要对另一项服务进行 10 到 50 次调用。这些调用是独立的并且可以并行化,所以我在考虑使用执行器服务来跨多个线程分配工作。

我想知道我是应该为每个请求实例化一个 executorService 还是应该在整个 webapp 中共享一个 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 请求(选项 1)实例化 executorService,因为服务器很快就会 运行 内存不足,而且动态创建线程池的成本很高(耗时).

因此您需要在服务器启动期间创建共享实例(选项 2)并根据您的要求配置线程池大小并进行性能测试。executorService =14=]

关于线程池可以参考here了解