GeeCON Prague 2014*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Thread-safe Servlets 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 "Thread-safe Servlets" Watch "Thread-safe Servlets" New topic
Author

Thread-safe Servlets

marlon tan
Ranch Hand

Joined: Feb 18, 2001
Posts: 53
Hi,
(1)
If a servlets implements SingleThreadedModel, then the servlet container would instantiate as many servlet instances as it needs right? If my servlet has a class variable myVar, then would these different servlet instances refer to the SAME myVar?
As a follow-up question to above paragraph, what difference does Instance variable make compared to Class variable? Can we say that Instance variable is thread safe and Class variable is not?
(2)
As indicated in the specification, "Objects that are accessible to more than one servlet instance at a time, such as instances of HttpSession, [and perhaps Context attributes,] may be available at any particular time to multiple servlets, including those that immplement SingleThreadedModel." It also says "Multipe servlets executing request threads may hae active access to a single sesin object at the same time. The developer has the responsibility for synchronizing access to session resources as appropriate."
Given the above paragraph, can I conclude that session attributes are not thread safe? How about context attributes?
Thank you very very much and more power!
Sincerely,
Marlon Tan
Velmurugan Periasamy
Ranch Hand

Joined: Nov 09, 2000
Posts: 95
Instance variables are thread-safe if the servlet implements SingleThreadModel. Class variables are not.
Session attributes are not thread-safe, context attributes also are not thread-safe.

------------------------------------------------------------------------------------
Velmurugan Periasamy
Sun Certified Java Programmer for Java 2 Platform
Sun Certified Web Component Developer for J2EE platform
Sybase Certified EAServer Developer (similar to jCert level 3)
------------------------------------------------------------------------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/
------------------------------------------------------------------------------------

[This message has been edited by Velmurugan Periasamy (edited October 15, 2001).]


------------------------------------------------------------------------------------<BR>Velmurugan Periasamy<BR>Sun Certified Java Programmer for Java 2 Platform<BR>Sun Certified Web Component Developer for J2EE platform<BR>Sybase Certified EAServer Developer (similar to jCert level 3)<BR>------------------------------------------------------------------------------------<BR>Study notes for Sun Java Certification<BR><A HREF="http://www.geocities.com/velmurugan_p/" TARGET=_blank rel="nofollow">http://www.geocities.com/velmurugan_p/</A><BR>------------------------------------------------------------------------------------
Tim Duncan
Ranch Hand

Joined: Aug 20, 2001
Posts: 150
Originally posted by marlon tan:
If a servlets implements SingleThreadedModel, then the servlet container would instantiate as many servlet instances as it needs right?

I think this is implementation dependent, but the container may do servlet pooling, so you can't rely on thread safety with static vars.

marlon tan
Ranch Hand

Joined: Feb 18, 2001
Posts: 53
How about request object?
Thank you very much for the help!
[This message has been edited by marlon tan (edited October 16, 2001).]
 
GeeCON Prague 2014
 
subject: Thread-safe Servlets