Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SingleThreadModel Vs Concurrency

 
gurneeraj singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know that SingleThreadModel is used for protecting instance variables. Does't it also affect the concurrency of service() method in servlet?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. It ensures that the service() method of any particular servlet instance will not be called concurrently by more than a single thread. In other words, there is no concurrency for servlet instances.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add here that there may still be multiple instances of the servlet after implementing the SingleThreadModel interface.
 
Samantha Halpagoda
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sandeep Bhandari:
Just to add here that there may still be multiple instances of the servlet after implementing the SingleThreadModel interface.


How could that happen? but in HFSJ it says there is only one instance of a servelt at anytime.. and that instance will be used in different threads, so if we have single thread model, is it possible to create/generate multiple instance of same servelt?
could you please explain this further?
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Samantha Halpagoda:


How could that happen? but in HFSJ it says there is only one instance of a servelt at anytime.. and that instance will be used in different threads, so if we have single thread model, is it possible to create/generate multiple instance of same servelt?
could you please explain this further?


Here's what SUN says:

In the case of a servlet that implements the SingleThreadModel interface, the servlet
container may instantiate multiple instances of that servlet so that it can handle a heavy
request load while still serializing requests to a single instance.


and a word of caution about assuming that everything is being single instantiated is also given by SUN in the words:

The use of the SingleThreadModel interface guarantees that one thread at a time will
execute through a given servlet instance�s service method. It is important to note that this
guarantee only applies to servlet instance. Objects that can be accessible to more than one
servlet instance at a time, such as instances of HttpSession, may be available to multiple
servlets, including those that implement SingleThreadModel, at any particular time.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that it's also possible to have multiple instances of the same servlet class if they are configured under different names. This has nothing to do with STM/non-STM, though.
[ August 06, 2008: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic