简体   繁体   English

Servlet:Singleton,Singlethread或Multi Instance Multithread

[英]Servlet: Singleton, Singlethread or Multi Instance Multithread

This question has been asked previously and discussed before but i want to ask it further. 之前已经问过这个问题并在之前讨论过,但我想进一步询问。

  1. Are Servlets Singleton or not ? Servlets是否是Singleton? According to me they are initialized only by the container but they are still not singleton ?? 据我说,他们只是由容器初始化,但他们仍然不是单身? why ?? 为什么?

  2. Are Servlets Single Thread or multi Threaded (Forget about javax.servlet.SingleThreadModel class) ie What happens when there are multiple requests for a single servlet ?? Servlet是单线程还是多线程(忘掉javax.servlet.SingleThreadModel类)即当单个servlet有多个请求时会发生什么? If they are executed conncurrently, that means it is multi threaded ?? 如果它们被并行执行,那意味着它是多线程的? and if its multi threaded then each thread will have an instance of the servlet, which contradicts with the 1st point !! 如果它的多线程,那么每个线程将有一个servlet的实例,这与第一点相矛盾!

What i think is, Whenever theres a new request, The container creates a new Thread for the incoming Request say Req1, in that it calls or dispatches the control to the service method of servlet. 我认为,每当有新的请求时,容器为传入的请求创建一个新的线程说Req1,因为它调用或调度控件到servlet的服务方法。 Now this execution happens concurrenlty.. i guess so... 现在这个执行发生了......我想是的......

Does my working stands the same in a MVC envirionment ?? 我的工作在MVC环境中是否相同? (say Struts 1/2, Springs) (比如Struts 1/2,Springs)

The fact that exists just one instance doesn't means it isn't multithread. 只存在一个实例的事实并不意味着它不是多线程的。 More thread can concurrently call tha same method of the same instance. 更多线程可以同时调用相同实例的相同方法。 Servlets are absolutly multithread. Servlet是绝对多线程的。

Servlets are multithreaded - this is the base for their efficiency. Servlet是多线程的 - 这是它们效率的基础。 One can use "implements SingleThreadModel" to make a servlet single-threaded, so for every request a new object will be created. 可以使用“implements SingleThreadModel”来使servlet成为单线程,因此对于每个请求都将创建一个新对象。

Recently this interface SingleThreadModel was deprecated 最近,这个接口SingleThreadModel已被弃用

It depends on the implementation. 这取决于实施。 The servlet interface doesn't specify. servlet接口未指定。 http://docs.oracle.com/javaee/5/api/javax/servlet/Servlet.html http://docs.oracle.com/javaee/5/api/javax/servlet/Servlet.html

However, if you see the HttpServlet then you can see it specifies that it should be synchronized. 但是,如果您看到HttpServlet,那么您可以看到它指定它应该同步。 http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServlet.html http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServlet.html

也许,下面链接中给出的图表说明了一切...... http://www.tutorialspoint.com/servlets/servlets-life-cycle.htm

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

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