This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

One clarification needed regarding Servlet

 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
reply
    Bookmark Topic Watch Topic
  • New Topic