JavaMemoryModel: init methods

From: Joseph Bowbeer (
Date: Mon Sep 25 2000 - 15:07:27 EDT

A couple questions about construction/initialization safety:

If I understand correctly, some of the proposed fixes to JMM are
designed so that immutable objects can be accessed without
synchronization once they are constructed.

Q1: These guarantees don't address bean-style construction (akin to
factory-style construction), right?

    For example, given:

        Bean bean = new Bean();
        return bean;

    If one thread initializes the bean and publishes it and another
thread accesses the bean without synch., then the accessing thread may
see an uninitialized bean.

Q2: Have any guarantees been proposed concerning the API's that rely on
'init' methods? Applets and Servlets fall into this category.

    public Servlet {
        int[] arr;
        public init() {
            arr = new int[] { 1, 2, 3, 4 };
        public service(request, response) {
            // access arr ...

If the API guarantees that 'init' is called "first", can we assume that
sufficient synchronization is being performed behind the scenes to make
this kind of thing thread-safe? Or should 'service' synch on 'arr'?

Joe Bowbeer

------------------------------- JavaMemoryModel mailing list -

This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:27 EDT