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 SingleThreadModel ?? 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 "SingleThreadModel ??" Watch "SingleThreadModel ??" New topic

SingleThreadModel ??

R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
is SingleThreadModel is thread safe ??
As this explanation is also true that it gaurantees only that two servlet will not execute service method at same time.
but any other thread might change the instance variable value. so it can not be said Thread safe.
Now problem is that in one of Mock I read that STM is not thread safe as per above given logic.
but at podar exam it says that instance var is thread safe.
which answer is correct ?? :confuse:

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
There is only one servlet instance per registered servlet. Normally, when concurrent requests are handled by the web server, threads are spawned which executes the service() method of the servlet. Instance variables are not thread safe, but local variables within the service() method are. The request object is also "localized" per thread.
According to the Servlet specs, when a servlet implements the SingleThreadModel interface (which is an empty "tag" interface) and concurrent requests are handled by the web server, the container is supposed to create a new servlet instance per request. It is possible that the container will pool these servlet instances. it is also possible that the container will serialize, i.e. queue, the requests per instance. It is possible that the container implementation is a combination of these.
Since there are now multiple servlet instances handling each request, the instance variables are now thread safe.
In either case, however, if any servlet static variables are defined, these will NOT be thread safe unless you explicitly make it so with custom code.
I agree. Here's the link:
subject: SingleThreadModel ??
It's not a secret anymore!