Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: Synchronization of the constructor Date: Sun, 14 Aug 2011 07:42:13 -0700 Organization: A noiseless patient Spider Lines: 71 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 14 Aug 2011 14:42:22 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="13005"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+YM5tt8V4Gnk8YgBhj2ifoD38zW46Nkog=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: Cancel-Lock: sha1:OYeYJBrGARLe1vxyqhh4vcCf5wE= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7107 On 8/13/2011 11:12 PM, kedar mhaswade wrote: > On Aug 13, 8:13 am, markspace<-@.> wrote: >> public class Immutable { >> private final SomeObject o; >> public Immutable() { >> o = new SomeObject(); >> } >> ... >> >> This is thread safe, and immutable, > Doesn't that depend on how Immutable SomeObject is and how Immutable's > immutability relates to it? No! That's the point. Even something like this: public class Stooges { private final ArrayList stooges; public Stooges() { ArrayList temp = new ArrayList(3); temp.add( "Larry" ); temp.add( "Moe" ); temp.add( "Curly" ); stooges = temp; } public List getStooges() { return new ArrayList( stooges ); } } Where "SomeObject" is known to be mutable (here, it's an ArrayList), is still thread safe and immutable. Thread safety and immutability here is just the absence of writes. That's all we have to do to to be safe. And Java guarantees that the inital writes in the constructor are "flushed" before any other thread can see them. Thus this object is thread safe. Note that I: 1. Don't modify any memory after I create the immutable object. I.e., "stooges" is never modified. 2. I don't allow the user to do so either. 3. I don't allow a reference to "stooges" to escape. If one were passed in, I'd be obliged to copy the whole thing. Otherwise, someone else could be holding on to the reference and modify the object behind my back, thus breaking immutability and thread safety. > Also, other standard caveats apply (e.g. Immutable should be declared > final). Nope. Certainly if you extend a class and then break its published constraints, you've done something bad. But that applies to every class in the system. The only caveat I'd apply is to document the immutability of the class. Once you've done that, you've done all you can do, really. In a public API, sure, declaring the class final is worth considering, but programmatically it's not required. Publishing the reference you get still has to be considered, but that is also for the user of this class to address. We simply can't do anything about it.