Can getAttribute() method of Tomcat ServletContext implementation be called without synchronization?

I would like to read some parameters during servlet initializtion (in init() method), and store them among servlet context attributes (using getServletContext().setAttribute()). I would like to read these parameters later - during some request processing (using getServletContext().getAttribute()). So, the multiple threads could do this simultaneously. My question is if such an attempt is safe? Could I be sure that multi threaded calls to the getAttribute() don't mess up any internal state of the servlet context?

Please take into account that I'm not going to call the setAttribute() anywhere besides the initialization. So, only calls to the getAttribute() are going to be done from multiple threads. But depending on the internal implementation, this also could be dangerous. So, any information about Tomcat's implementation would be appreciated.

Answers


The primary implementation of ServletContext in Tomcat is the ApplicationContext class. If you look at the linked resource, you will see that the attributes are stored in a java.util.concurrent.ConcurrentHashMap instance. So, for ApplicationContext at least the getAttribute() and setAttribute() methods are thread-safe.


Need Your Help

Is Validation a SemiGroup/Monoid : using |+| does not work

scalaz

I was uder the impression that Validation could be used as a Monoid/SemiGroup

Youtube video id (or URL) to "user" and "channel"

youtube youtube-api youtube-data-api android-youtube-api youtube-channels

Given a URL or ID of a Youtube video, is it possible to get the (1) name of the "user" who had uploaded it and (2) channel that it belongs to? If so, what is the API request?