• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread-safe Servlets

 
marlon tan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Tim Duncan
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about request object?
Thank you very much for the help!
[This message has been edited by marlon tan (edited October 16, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic