Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One clarification needed regarding Servlet

 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic