Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7107

Re: Synchronization of the constructor

From markspace <-@.>
Newsgroups comp.lang.java.programmer
Subject Re: Synchronization of the constructor
Date 2011-08-14 07:42 -0700
Organization A noiseless patient Spider
Message-ID <j28msd$cmd$1@dont-email.me> (permalink)
References <j25hro$cjf$1@news.onet.pl> <j25uri$vm$1@dont-email.me> <j264b6$3u5$1@dont-email.me> <e46c0cd1-9906-4195-9d30-070318fa5318@y39g2000prd.googlegroups.com>

Show all headers | View raw


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.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Synchronization of the constructor MaciekL <__nospam__maclab@o2.pl> - 2011-08-13 11:58 +0200
  Re: Synchronization of the constructor Robert Klemme <shortcutter@googlemail.com> - 2011-08-13 12:17 +0200
    Re: Synchronization of the constructor "Qu0ll" <Qu0llSixFour@gmail.com> - 2011-08-13 21:36 +1000
      Re: Synchronization of the constructor Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-13 09:30 -0300
    [OT] Natural language. Was: Re: Synchronization of the constructor Patricia Shanahan <pats@acm.org> - 2011-08-13 07:10 -0700
  Re: Synchronization of the constructor Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-13 09:36 -0400
    Re: Synchronization of the constructor markspace <-@.> - 2011-08-13 08:13 -0700
      Re: Synchronization of the constructor Lew <lewbloch@gmail.com> - 2011-08-13 09:23 -0700
        Re: Synchronization of the constructor Patricia Shanahan <pats@acm.org> - 2011-08-13 10:04 -0700
          Re: Synchronization of the constructor Lew <lewbloch@gmail.com> - 2011-08-13 21:29 -0700
            Re: Synchronization of the constructor kedar mhaswade <kedar.mhaswade@gmail.com> - 2011-08-13 23:16 -0700
            Re: Synchronization of the constructor "Qu0ll" <Qu0llSixFour@gmail.com> - 2011-08-15 14:24 +1000
              Re: Synchronization of the constructor Lew <lewbloch@gmail.com> - 2011-08-14 23:03 -0700
      Re: Synchronization of the constructor kedar mhaswade <kedar.mhaswade@gmail.com> - 2011-08-13 23:12 -0700
        Re: Synchronization of the constructor markspace <-@.> - 2011-08-14 07:42 -0700
          Re: Synchronization of the constructor markspace <-@.> - 2011-08-14 07:51 -0700
            Re: Synchronization of the constructor Lew <lewbloch@gmail.com> - 2011-08-14 09:10 -0700
              Re: Synchronization of the constructor markspace <-@.> - 2011-08-14 11:28 -0700

csiph-web