java 10 个客户端请求一个 Servlet。创建了多少个 servlet 实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10577414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:38:49  来源:igfitidea点击:

10 clients requests for a Servlet.How many servlet instances are created

javajspjakarta-eeservlets

提问by user1281029

Suppose 10 clients requests for a Servlet. How many servlet instances are created? Also will it create any concurrency modification problem if each instance is trying to make changes? Please help.

假设 10 个客户端请求一个 Servlet。创建了多少个 servlet 实例?如果每个实例都试图进行更改,它还会产生任何并发修改问题吗?请帮忙。

采纳答案by Jigar Joshi

Only one instance of servlet exist (per classloader) , and each request will be served on its own thread

只存在一个 servlet 实例(每个类加载器),并且每个请求都将在其自己的线程上提供服务

So is there any thing that is shared amongst requests you need to manage synchronization

那么在您需要管理同步的请求之间共享任何东西吗?

回答by user1280078

Servlet is instanciated as singleton in servlet container, thats why we can't declare global variables in it.

servlet 在 servlet 容器中被实例化为单例,这就是我们不能在其中声明全局变量的原因。