File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Multiple instances and multiple threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Multiple instances and multiple threads" Watch "Multiple instances and multiple threads" New topic
Author

Multiple instances and multiple threads

Smita Chopra
Ranch Hand

Joined: Jun 27, 2005
Posts: 45
I have read that for a servlet, only one instance is created and when multiple concurrent requests come in, multiple threads are created, one for each request.
But when the Servlet implements SingleThreadModel multiple instances are created rather than multiple threads.
Can someone please tell me what's the difference between multiple threads and multiple instances in case of a servlet?
Satish Chilukuri
Ranch Hand

Joined: Jun 23, 2005
Posts: 266
In case of a single servlet instance, the jvm will create only one servlet instance and whenever a new request comes in, it will spawn a new thread which will operate on this servlet instance.

The best way to understand this is to have a non static instance variable as a counter in the servlet and increment it in the doGet or doPost methods. The count will always be equal to the total no. of requests handled by the servlet since its instantiation.

In case of a SingleThreadModel, for each incoming request, a new instance of the servlet is created. Here if you increment the counter and print it in the doGet or doPost methods, the value will always be 1.
Parag Mirja
Greenhorn

Joined: Jun 07, 2005
Posts: 2
I think this confusion is what made sun deprecate the SingleThreadModel in Servlet 2.4 spec.
See below the Javadoc for details.
Note that SingleThreadModel does not solve all thread safety issues. For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used. It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources. This interface is deprecated in Servlet API version 2.4.
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Originally posted by Smita Chopra:
Can someone please tell me what's the difference between multiple threads and multiple instances in case of a servlet?


You have read correct. Multiple instances refers to having a new servlet object created (by the container) for each request. This has certain overheads in terms of object creation/destruction and the like. The alternative is to have a multithreaded model in which a new thread is created (as oppossed to a new object). A single servlet object would support multiple threads. As noted by Parag, the SingleThreadedModel has been depracated and is best ignored.
[ July 05, 2005: Message edited by: Sharad Agarwal ]

Alco-Haul: We move spirits.
Demented Deliberations of a Dilettante
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Sharad Agarwal:


You have read correct. Multiple instances refers to having a new servlet object created (by the container) for each request. This has certain overheads in terms of object creation/destruction and the like. The alternative is to have a multithreaded model in which a new thread is created (as oppossed to a new object). A single servlet object would support multiple threads. As noted by Parag, the SingleThreadedModel has been depracated and is best ignored.

[ July 05, 2005: Message edited by: Sharad Agarwal ]


Most servlet containers pool servlet instances (I know Tomcat does) of singleThreaded servlets so there isn't a huge penalty for using them.

I wouldn't make the decision based on performance.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Originally posted by Ben Souther:
Most servlet containers pool servlet instances (I know Tomcat does) of singleThreaded servlets so there isn't a huge penalty for using them.

I wouldn't make the decision based on performance.


Neither would I. The note that Parag attached says it all. The SingleThreadModel does not buy us thread safety AND it introduces an overhead (however small). I think it was William Brogden who pointed out to me (quite emphatically) in a previous thread that there is NO justification for use of the SingleThreadModel. It does seem a retrogressive interface and has been depracated. Using it in your webapp may make future upgrades very, very difficult.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Yes, 'that it is deprecated' is the best reason NOT to use it.
Smita Chopra
Ranch Hand

Joined: Jun 27, 2005
Posts: 45
Thanks everyone for replying.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple instances and multiple threads