Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Number of Servlet Instances for SingleThreadModel

 
Bhavik Patel
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to clear one thing about the Instances running for SingleThreadModel

Let's say i am implementing SingleThreadModel ..What approach my container will take?? Is it create Only Single instance for the specific servlet and queues up the request to that servlet OR Is it create multiple instances to serve each requests..Which approach container takes...Is it Container dependendent ..means Tomcat & Jboss can follow different mechanisms..When we can say Container will definately create multiple instance for specific servlet Whether its on same JVM or its distributed environment..Can any one clear this thing.. I went to servlet specs SRV 2.2 page 24 at bottom ..Number of Instances..They have used " May " clause ...Can anyone clear this thing??

Thanks
 
singh santosh
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
first let me tell u onething ,whether u implement SingleThread Model(a really bad thing) or not your container will create only one instance of a specific SERVLET (valid for all container.),only with each request a new thread will be created .

Yes ,when u implement SingleThreadModel ,u are telling ur container to just have only one thread for this servlet(process one request at a time)

Each jvm (in distributed world )will have one instance per servlet

so never say this .." multiple instance for specific servlet ".its a sin in servlet society.


regards

-santosh
SCJP1.4(96%)
SCWCD -preparing
[ February 22, 2006: Message edited by: singh santosh ]
 
Ramya Iyer
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singh,

pls go thro the servlet spec. for SingleThreadModel, multiple instances of the servlet would be available based on the request load.

In the case of all other servlets (that do not implement SingleThreadModel) there would be single instance per JVM and multiple threads.

Regards,
Ramya.
 
Bhavik Patel
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes .. i agree that its possible that container can create multiple instance on the request load..but that is container dependent ...Different container can handle same request load differently ..correct me if wrong
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes .. I agree that its possible that container can create multiple instance on the request load..but that is container dependent ...Different container can handle same request load differently ..correct me if wrong


If your servlet implements SingleThreadModel then for every single request that it receives a new instance of the servlet will be created and assigned to the thread, since no more than one thread can access the service method.
 
P Chittaranjan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually container creates one instance per one servlet defined in web.xml.
Implementing SingleThreadModel will not guaranty to create multiple servlet instances.But it may create multiple instances depending upon the request load.
Implementing SingleThreadModel is same as synchronizing the service method.
So all the requests will be serialized for exceution of the service method on a perticular servlet instance.

Since the servlet instance is synchronized, at a time it can service one request. So other requests will be waiting in the queue for the execution.

If requests are keep comming , the queue will keep growing.
It will eat the memory of the container.
To release the request load, the container may create some more instance of the servlet and serialize the request call on that instance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic