[concurrency-interest] Static initializer

Hanson Char hanson.char at gmail.com
Tue Apr 10 14:23:28 EDT 2007


I would make the static "instance" field final as well in this case.

My 2 cents.

Hanson Char

On 4/10/07, Mike Quilleash <mike.quilleash at subexazure.com> wrote:
>
>
> Hi all,
>
> Just a quick question about static initializers.  I've read JCiP (excellent
> book BTW) and wondered if the following is threadsafe.  The examples in the
> book didn't quite cover this case (code/method calls in the constructor).
>
>
> public class MyClass
> {
>    private static MyClass instance = new MyClass();
>
>    private final Map map;
>
>    private MyClass()
>    {
>
>       // do some init work
>
>       map = Collections.unmodifiableMap( ... );   }
>
>    public static MyClass getInstance()
>    {
>        return instance;
>    }
>
>    // other accessor method(s) to access info in the map
> }
>
>
> Is any thread that calls getInstance() guaranteed to see a fully initialized
> MyClass instance?  If so, is this because of the static initialization, or
> the "final" someObject, or both?  I'd like to know before I go and use this
> pattern everywhere in my code!
>
> Thanks.
>
> Mike.
>   This e-mail is bound by the terms and conditions described at
> http://www.subexazure.com/mail-disclaimer.html
>
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>


More information about the Concurrency-interest mailing list