File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Single Thread Model. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Single Thread Model." Watch "Single Thread Model." New topic

Single Thread Model.

Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 389
What does Single Thread Model exactly mean?

Is it...only one thread/one instance at a time
is 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?

SCJP 1.4, SCWCD 1.4, SCBCD 1.5, TOGAF 9, Comptia Cloud+
vandu matcha
Ranch Hand

Joined: Dec 28, 2005
Posts: 57
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

Joined: Nov 05, 2005
Posts: 76
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 !!!

SCJP, SCWCD, SCBCD, IBM XML, IBM OOAD, SCEA 5, ITAC - Distinguished IT Architect.
Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
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

when you really want something, all the universe always conspires in your favour.<br /> <br />SCJP1.5-77%<br />SCWCD-89%
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
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

Joined: Mar 27, 2006
Posts: 245
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 ]

Vikrant Pandit
Sathish Nagappan
Ranch Hand

Joined: Nov 05, 2005
Posts: 76
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

Joined: Jul 03, 2006
Posts: 22
Is STM included in 310-081 ?

SCJP 1.4 - 95% Passed<br />SCWCD 1.4 - 89% Passed
I agree. Here's the link:
subject: Single Thread Model.
It's not a secret anymore!