This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple instances and multiple threads

 
Smita Chopra
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sharad Agarwal
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, 'that it is deprecated' is the best reason NOT to use it.
 
Smita Chopra
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for replying.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic