Marcel wrote: > > Marc Speck wrote: > > Actually, this internal field is the source of a potential data race: > > thread A runs up to the point, when it has constructed > > LocatorFactoryImplEx but has not yet assigned the internal field. > > Thread A gets on hold relative to thread B. B runs through the getter > > and sees that locatorFactory is not null, fetches the object, access the > > uninitialized internal field and voilĂ ! > > We have a mess. > > the revised memory model in java 1.5 guarantees that final fields are > properly assigned after the constructor finishes. other threads accessing the > constructed locator factory instance will always see the correct field value. > > see: http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#finalWrong
That's true, if there's only final fields involved. The setting the reference in the getter is atomic so at worst there will be duplicate factories for a while. But there a situation where one thread can be using one locator factory and another thread using a different one isn't there? May not be a problem in the example of a simple factory but can't be good in more complex examples. > > regards > marcel REED ELSEVIER (UK) LIMITED - Registered office - 1-3 STRAND, LONDON WC2N 5JR Registered in England - Company No. 02746621
