wood burning stoves 2.0*
The moose likes Servlets and the fly likes One clarification needed regarding Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "One clarification needed regarding Servlet" Watch "One clarification needed regarding Servlet" New topic
Author

One clarification needed regarding Servlet

Jay Shukla
Ranch Hand

Joined: Jun 08, 2008
Posts: 214
Hi I'm preparing for SCWCD Exam and for that i'm taking help of "Head First Servlet and JSP" in that request and response topic that have told

"You might hear people saying "Each instace of the servlet..." but that's just wrong. There aren't multiple instances of any servlet,except one in special case of SingleThreadModel""


As per my understanding i think in SingleThreadModel there are multiple instaces of Single Servlet? Am I Correct???

Can you just explain me the above Quotation in brief please?

Thanks in Advance.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Firstly, the SingleThreadModel is deprecated and not recommended for use.
The preferred implementation for the STM is to have a pool of servlet instances (ie more than one instance) and make sure that only a single thread accesses any one instance at a time.
pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
Hi
WebContainer creates only one instance of your servlet for handling all the requests by creating multiple threads .It means one thread run for your one request and other threads handles other requests.This is the case when we dont implement SingleThreadModel.ONE THREAD PER REQUEST

But if we implement SingleThreadModel ,then webcontainer create only one thread per instance .It means it can handle only one request at a time.If many requests comes at a time ,then it creates multiple instances (each instance has only one Thread ,thats why SingleThread is its named)to handle multiple requests.ONE INSTANCE HAVING ONLY ONE THREAD PER REQUEST

Hope this will clear your concept.
Sorry if any English grammatical mistake.


SCJP 5.0(75%), SCWCD 5.0(88%)
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by pradeep singh:

But if we implement SingleThreadModel ,then webcontainer create only one thread per instance .It means it can handle only one request at a time.If many requests comes at a time ,then it creates multiple instances (each instance has only one Thread ,thats why SingleThread is its named)to handle multiple requests.ONE INSTANCE HAVING ONLY ONE THREAD PER REQUEST


No, see the API:

If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet's service method. The servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet.


There is more than one way to implement this behaviour, having a pool is preferred as it allows multiple threads to be served at once. Also, it has nothing to do with thread creation. Threads also tend to be managed from a pool rather than constantly creating and deleting thread instances.
Jay Shukla
Ranch Hand

Joined: Jun 08, 2008
Posts: 214
As per the above explanation given by David and Pradeep I think ---

"In SingleThreadModel there are multiple thread and multiple instance for one servlet but at a time onluy one thread is running ; While in MultiThreadModel there are only one instance per servlet and there are many thread for each of request running simultenously."

Is my above understanding absolutly right???

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42374
    
  64
In SingleThreadModel there are multiple thread and multiple instance for one servlet but at a time onluy one thread is running

Not necessarily. As David pointed out above, there are other ways to implement STM, like serializing requests. In that case, only a single instance would be necessary.

While in MultiThreadModel there are only one instance per servlet and there are many thread for each of request running simultenously.

Correct. (Note that there may still be multiple instances of a particular servlet class if the servlet is configured multiple times in the web.xml file.)


Ping & DNS - my free Android networking tools app
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: One clarification needed regarding Servlet