据我了解,像 jboss 这样的 Web 容器有一个参数来提及要创建的 servlet 线程数,这是容器可以创建在 servlet 的单个实例上运行的最大线程数。
但是我的问题是,有没有办法指定要创建的 servlet 实例的数量?如果是,目的是什么?当单个实例上运行的一个或多个线程以某种方式损坏该 servlet 数据或 destroy() 它时,这种需要可能是为了处理请求 - 冗余目的。
最佳答案
编辑:小心!从 Servlet 规范 2.4 开始,接口(interface) javax.servlet.SingleThreadModel已弃用,没有直接替代。
规范并不清楚容器是否应该创建同一个 servlet 的多个实例,或者始终只创建一个实例。
来自 Servlet 规范 2.4 http://download.oracle.com/otndocs/jcp/servlet-2.4-fr-spec-oth-JSpec/
SRV.2.2 Number of Instances
The servlet declaration which is part of the deployment descriptor of theWeb application containing the servlet, as described in Chapter SRV.13, “Deployment Descriptor”, controls how the servlet container provides instances of the servlet. For a servlet not hosted in a distributed environment (the default), the servlet container must use only one instance per servlet declaration. However, for a servlet implementing the SingleThreadModel interface, the servlet container may instantiate multiple instances to handle a heavy request load and serialize requests to a particular instance.
In the case where a servlet was deployed as part of an application marked in the deployment descriptor as distributable, a container may have only one instance per servlet declaration per Java Virtual Machine (JVMTM). However, if the servlet in a distributable application implements the SingleThreadModel interface, the container may instantiate multiple instances of that servlet in each JVM of the container.
您可以在文档中找到有关单线程模型的更多信息
关于multithreading - Servlet 线程池 vs Servlet 实例池 - 通过 Web 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7826452/