• Post Reply Bookmark Topic Watch Topic
  • New Topic

Single Threaded Servlets

 
Harry Singh
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Java Gurus,
Can anyone tell me about the thread safe model in servlets. Well does this mean that if i am implementing threadsafe interface then only one instance of that servlet will be made by container.. or does this mean that there can still be multiple instances on servlets made by container and then there will be something like servlet Pool...
thanks in advance
Regards,
Harry
 
Kaustubh Patil
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't get confused between the "SingleThreadedModel" and the instances of the servlets.
How many instances of the servlets should the servlet engine create is decided by the sevlet engine itself. The servlet engine might use a single instance of the servlet to serve multiple requests at its own discretion.
What "SingleThreaded" essentially means is that the programmer is instructing the servlet engine to use one instance of the servlet to serve only one request at a time. This is as good as synchronozing the service method. There is however no restriction on the number of servlet instances.
 
Andy Bowes
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
The situation when using "SingleThreadedModel" is rather worse than Kaustubh portrays. If a servlet is marked as being Single Threaded the servlet engine will create a separate instance of the servlet class for each request handled by the servlet. This is obviously not very desireable
The best practice is basically to forget that this interface exists at all and code you servlets so that they are thread-safe. Basically try and avoid the use of instance variables in the servlet classes especially those whose value can be modified during the service() methods.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The spec says that SingleThreadModel servlets can be 'pooled', so a new servlet is not necessarily created with each request. The container might instantiate a pool of them and serialize requests through a given instance.

Having said that, take the advice about ignoring th is interface. It will be deprecated with the next release of servlets (2.4)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!