java - Thread-safety of javax.servlet.Servlet#getServletConfig() -
i wondering why calling javax.servlet.servlet#getservletconfig()
thread safe: if check implementation in javax.servlet.genericservlet
servlet api 3.0.1, see following:
package javax.servlet; // lines omitted public abstract class genericservlet implements servlet, servletconfig, java.io.serializable { // lines omitted private transient servletconfig config; // lines omitted public void init(servletconfig config) throws servletexception { this.config = config; this.init(); } // lines omitted public servletconfig getservletconfig() { return config; } // lines omitted }
the field config
written in init(servletconfig)
without synchronization, locking or config
being volatile, while getservletconfig()
called anytime later worker thread of servlet container. took quick tomcat/catalina code base, see no indication of servlet container performing magic, guarantee thread safe access field config
through getservletconfig()
e.g. javax.servlet.genericservlet#service(servletrequest, servletresponse)
, corresponding methods in javax.servlet.http.httpservlet
.
am missing here? guarantee if thread used init(servletconfig)
thread b calling getservletconfig()
see correct state of field config
(java "happens-before"), , not e.g. null
?
servletconfig
singleton object (managed container) , use retrieve init-parameters
or servletcontext
or servletname set during servlet init
phase. can't write servletconfig
object, rather container alone manages object, so, thread safe.
the field config written in init(servletconfig) without synchronization, locking or config being volatile, while getservletconfig() called anytime later worker thread of servlet container ?
genericservlet
abstract class container not create object, rather container creates object (only single instance default) custom httpservlet
class write/provide initializes servletconfig
object.
the question thread-safety of getservletconfig
() not of servletconfig
?
once container creates servletconfig
object during servlet lifecycle's init
phase, never changed. so, getservletconfig
() thread safe (i.e., not matter how many threads reading servletconfig
object long none of threads allowed write it).
updated question:
what guarantee if thread used init(servletconfig) thread b calling getservletconfig() see correct state of field config (java "happens-before"), , not e.g. null?
the servlet container calls init method once after instantiating servlet. the init method must complete before servlet can receive requests. servlet container cannot place servlet service if init method throws servletexception or not return within time period defined web server. can @ here
the creation of user request threads happens after completion of init() method successfully. so, there no need data (servletconfig
) synchronization between user request threads because the servletconfig
object made available before user request threads have been created.
Comments
Post a Comment