File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

SingleThreadModel

Aleks Pascoal
Greenhorn

Joined: Jul 16, 2002
Posts: 12
I got a question in JWebPlus witch states that servlets implementing SingleThreadModel are ThreadSafe. Ok, there is gonna be only on thread at time executing service method, what about Session Attributes, ServletContext Attributes and Classes Variables?
Thanks,
Aleks
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I agree. SingleThreadModel does little to help avoid threading issues outside the servlet itself.
Anyone who claims that SingleThreadModel is anything other than a hack to help with some sorts of legacy code access problems is overstating its benefits.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Shashank Tanksali
Ranch Hand

Joined: Feb 21, 2001
Posts: 122
I got a question in JWebPlus witch states that servlets implementing SingleThreadModel are ThreadSafe. Ok, there is gonna be only one thread at time executing service method, what about Session Attributes, ServletContext Attributes and Classes Variables?

For obvious reasons, the session and servlet context cannot be made threadsafe automatically. The only way, that would have been possible is if all invocations have their own copies of session and servlet context, which is simply not possible.
Synchronizing access to resources shared between servlets simply cannot be done by any container and will always remain the responsibility of the developer.
Implementing SingleThreadModel will not even prevent static variables from being modified by several servlets at the same time.
Shashank


<b><br /><a href="http://mailto:tanksali@hotmail.com" target="_blank" rel="nofollow">Shashank Tanksali</a><br /><i><br />SCJP2,SCJD2,SCWCD<br />IBM Certified Developer - XML and Related Technologies<br />PMI Certified Project Management Professional<br /></i><br /></b>
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3324
    
    8
Hi Aleks,
There was a similar doubt posted earlier also. I don't think it is mentioned anywhere in JWebPlus that a Servlet is thread safe if it implements STM. Can you please tell me the question id where it says that?
thanks,
Paul.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Aleks Pascoal
Greenhorn

Joined: Jul 16, 2002
Posts: 12
Thanks everybody, but anyone can say yes or no for sure to my question?
Aleks
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
I would say yes because the servlet instance itself is infact thread safe.


SCJP2
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.
The same goes for any external resources your servlets access.
Tauqueer Ali
Ranch Hand

Joined: Sep 05, 2001
Posts: 53
Aleks
Thread safety means, shared resources are not accessed simulataneously, thereby avoiding data corruption. In the case of a thread safe servlet, a shared resource could be an instance variable.
And to avoid data corrpution you need to synchronize its access. This is the normal way
of providing thread safety. But it doesn't mean that to avoid concurrent access, you start creating new objects for every request.
However, if your servlet implements SingleThreadModel interface, it essentially does that - creates a new instance of servlet object for every request. This way the instace variables are safe but they are not shared, and do not serve any purpose for which threads are provided in the first place.
Consider a small example:
If you 've a counter which counts the number of times ur servlet has been accessed, then you will make it an instance variable and you would like it to be thread safe at the same time. Here the SingleThreadModel is of no use. 'coz every instance of ur servlet will have its own counter variable. In this case you have no choice but to synchronize the access to counter varible programatically.
Hence, SingleThreadModel does make you servlet thread safe as it creates a new instance for every request, but its useless as it doesn't serve the purpose for which you might want to make ur servlet thread safe in the first place.


SCJP2, SCWCD, XML, OOAD<br />Kolkata, India
Shashank Tanksali
Ranch Hand

Joined: Feb 21, 2001
Posts: 122
Hence, SingleThreadModel does make you servlet thread safe as it creates a new instance for every request, but its useless as it doesn't serve the purpose for which you might want to make ur servlet thread safe in the first place.

My understanding is that a new instance is not created for every request. Rather the requests are all handled in sequence on the same instance.
Of course, the container might maintain several instances if there are lots of requests for a single servlet. However it still does not create a new instance for every request, which could be a really expensive operation and would adversely affect the performance.
Shashank
Tauqueer Ali
Ranch Hand

Joined: Sep 05, 2001
Posts: 53
shashank
"Rather the requests are all handled in sequence on the same instance."
This is exactly what happens in a simple servlet which doesn't implement SingleThreadModel. Only one instance of the servlet object is created. and instance variables persist across the requests.
In SingleThreadModel, as u mentioned it may be possible that the container uses the same instance of servlet instead of creating a new instance for every request. But this is called pooling in which the state of instance is reset.
this does not mean
"the requests are all handled in sequence on the same instance."
Shashank Tanksali
Ranch Hand

Joined: Feb 21, 2001
Posts: 122
"Rather the requests are all handled in sequence on the same instance."
This is exactly what happens in a simple servlet which doesn't implement SingleThreadModel.

I think that is not right.
When a servlet does not implement SingleThreadModel, the container handles the requests in parallel, not in sequence as you imply since by not implementing the SingleThreadModel, you are telling the container that your servlet is threadsafe and it is okay to have several invocations of the servlet in different threads at the same time.

Hence, SingleThreadModel does make you servlet thread safe as it creates a new instance for every request

What I was implying from my earlier post was that a new instance is not created for every request as you mention, but a currently free instance is reused.
Shashank
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
My understanding is that a new instance is not created for every request. Rather the requests are all handled in sequence on the same instance.

I think it depends on the servlet container.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SingleThreadModel