[concurrency-interest] Spring DispatcherServlet safe publication question

Jed Wesley-Smith jed at atlassian.com
Mon Mar 10 02:32:40 EDT 2008

David Holmes wrote:
> Jed Wesley-Smith wrote:
>> The key being "after ... properly initialized" - I would
>> posit that most servlet containers would implement a _happens before_
>> relationship here,  therefore all will be well.
> I would posit that servlet containers would need to implement a
> happens-before relationship here, but whether the writers of such servlet
> containers are actually aware of that need, is a different matter entirely.
> ;-)

hehe. My initial posit was based on the assumption (dangerous I know) 
that there is quite likely _some kind_ of synchronisation around the 
initialisation of the Servlet instances that causes this happens-before 
relationship _not necessarily directly related to the spec_. I.e. 
Servlets generally work as much perhaps by accident as by design :-)

As support for this argument I was going to present Tomcat's 
org.apache.catalina.core.StandardWrapper class that caches a Servlet 
instance (if it doesn't implement SingleThreadedModel). When I looked I 
saw the following:

    public Servlet allocate() {
            // Load and initialize our instance if necessary
            if (instance == null) {
                synchronized (this) {
                    if (instance == null) {
                        try {
                            instance = loadServlet();
                        } catch (Exceptions etc) {}
            return (instance);

The instance variable is (of course) not volatile.

I am now backing away slowly and quietly.

> Safe-publication is the forgotten topic of "Threads 101" :(

Unfortunately and clearly true.

More information about the Concurrency-interest mailing list