• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Single Thread Model.

 
Nikhil Jain
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does Single Thread Model exactly mean?

Is it...only one thread/one instance at a time
or
is it...one thread/instance at a time...

I mean whenever a request comes, will the container create a new instance of the servlet? or will it create a thread & wait for the thread to complete b4 another request is taken up?
 
vandu matcha
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Single threaded model means single thread multiple instances .....because the main thing in the single thread model is not to support multiple threads....hence single thread....inorder to support multiple requests it may use multiple instances .......Hope it helps to some extent
 
Sathish Nagappan
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To put in a different way ,
STM means that every servlet will have just one Instance and one thread at any point in time. That is how it guarantees Thread Safety for the instance variables of the servlet , but request attributes and session attributes and Servlet Context attributes are not safe because they can be used by JSPs too and other Servlets (even if the other Servlets Implement STM , they still have access to the attribute).
Besides due to this you cannot have multiple instances of a servlet running in the same JVM (even as heavy weight processes) and hence the webserver cannot service multiple clients simultaneously.

The bottom line is : DO NOT USE STM !!!
 
Senthil Kumar
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fernado,
Does this implies that there can be maximum of one request for
the STM servlet at a time?.Becaz we can't have mutiple threads and even mutiple instances for a STM servlet.then how does the container handles when there are more than one request for STM servlet is coming concurrently.


with thanks
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
STM means that every servlet will have just one Instance and one thread at any point in time.

Is that really so? My understanding was that there can be multiple instances, each serving a single request -and thus being single-threaded-, just like Vandu said.
 
Vikrant Pandit
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Single Thread Model simply means

ONLY ONE THREAD running on ONE instance of a servlet

This can be implemented in many ways

1) Servlet container creates only one instance of the servlet.
This way only one request for servlet can be serviced at a time

2) To improve performance, sometimes servlet container will create a pool of servlet instances . Only one thread will be running on one of the servlet instances at a given point of time

Thus, we cannot be sure that even instance variables are thread safe in STM

And request attributes are thread safe,because a request object is only being processed by only one thread at any given instance of time...
[ June 28, 2006: Message edited by: Vivek Pandey ]
 
Sathish Nagappan
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think so, The Head First JSP and Servlets book says that , if you are using STM or isThreadSafe in your JSP , then you are kissing your concurrency goodbye.
Now if there were multiple instances of a servlet ,each having a single thread just as you (Vandu and Ulf) said, then you can still have concurrent requests , only thing is the overhead that for each servlet the container creates multiple objects which consumes space on the RAM like EJB's. Hence you have multiple instances and instance variables become thread safe. But all the other variable scopes are not. ServletContext , HttpServletRequest and HttpSession , cos it can pass through other Web components and are not limited to servlets.

Either the HFSJ book's 'you are kissing your concurrency goodbye and STM brings a web server to its knees cos it can only service a single client at a time' is wrong or That is container/vendor dependent.
[ July 03, 2006: Message edited by: ashwanth fernando ]
 
Yi Yuan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is STM included in 310-081 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic