[concurrency-interest] synchronized on construction

Pete Soper Pete.Soper at Sun.COM
Wed May 24 09:43:55 EDT 2006

Thomas Hawtin wrote:
> Jeremy Manson wrote:
>>Jason Mehrens wrote:
>>>Browsing over the java.util.Vector class I noticed that in the constructors, 
>>>the initial writes to the internal data members (non-final and non-volatile) 
>>>are not performed under a synchronized block.   The same is true for when a 
> Under the new JMM would a synchronised block in the constructor 
> guarantee to fix the problem? Does the racy write of the Vector 
> reference, outside of the synchronised block, necessarily come after the 
> block itself. Could it technically be moved up before the lock is 
> acquired? Or am I misinterpreting something?
>>>Vector is deserialized.  Where does happens-before edge occur so that other 
>>>threads don't see uninitialized values of the internals?  The only happens 
>>>before-edges I can think of are during handoff between threads or on the 
>>>call to Thread.start().  Is how this class conforms to the JMM or is 
>>>something else going on?
>>Well, bear in mind that only one thread has access to instances of these 
>>classes during construction.  JMM problems only come up here if you pass 
>>a reference to the object to another thread via a data race.  These 
>>classes are not designed to work correctly if you do that - only the 
>>immutable ones, like String, really are.
>>If you are using this class, it is therefore imperative to provide 
>>proper synchronization during a handoff.
> Bug 6379897 deals with the thread safety of Random's seed when 
> constructed lazily in Collections. It's interested to see that the fix 
> for that attempts to make the use of seed thread-safe, rather than 
> construct the Random safely in Collections. Presumably the details of 
> that fix (setting of the seed value) require that my first paragraph is 
> wrong...
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6379897

There is a special guarantee extended to final fields related to 
construction that the experts can explain better than me. And as far as 
I can tell there is no possibility of "this" being published before the 
constructor completes. So this (current Mustang source for) Random looks 
looks OK and I think the attempt was successful.


> Tom Hawtin
> _______________________________________________
> 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