[concurrency-interest] Joda-Time immutability

Zhong Yu zhong.j.yu at gmail.com
Wed Aug 3 04:42:53 EDT 2011


The fix with `volatile` is incorrect. Another thread may observe an
uninitialized field - writing the volatile field and publishing `this`
reference can be reordered. Only a `final` field can prevent that.

The original problem is not about making updates to the field visible
to other thread - MutableDateTime was never intended to be thread
safe.

My understanding is that it can be fixed like this:

    class Base // immutable

        final state;

        getState(){ return state; }

        method(){ calculate( getState() ); }

    class Derived extends Base // mutable

        my_state;

        setState( state ){ my_state=state; }

        @Override
        getState(){ return my_state? my_state : super.getState(); }



- Zhong Yu


More information about the Concurrency-interest mailing list