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


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

Re: Synchronization of the constructor

From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Synchronization of the constructor
Date 2011-08-14 09:10 -0700
Organization http://groups.google.com
Message-ID <1b39f46d-0268-413a-9383-3a4a2c73920d@glegroupsg2000goo.googlegroups.com> (permalink)
References (1 earlier) <j25uri$vm$1@dont-email.me> <j264b6$3u5$1@dont-email.me> <e46c0cd1-9906-4195-9d30-070318fa5318@y39g2000prd.googlegroups.com> <j28msd$cmd$1@dont-email.me> <j28nec$ggg$1@dont-email.me>

Show all headers | View raw


markspace wrote:
>>   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 );
>>     }
>> }
> 
> I want to make one small change here.  The above is correct, but 
> misleading.  Both of the following are also immutable and thread safe:
> 
> 
>   public class Stooges {
> 
>       private final ArrayList stooges;
> 
>       public Stooges() {
>         ArrayList stooges = new ArrayList(3);
>         stooges.add( "Larry" );
>         stooges.add( "Moe" );
>         stooges.add( "Curly" );
>       }
>       public List getStooges() {
>         return new ArrayList( stooges );
>       }
> }
> 
> 
> -- or --
> 
>   public class Stooges {
> 
>       private final ArrayList stooges = new ArrayList(3);
> 
>       public Stooges() {
>         stooges.add( "Larry" );
>         stooges.add( "Moe" );
>         stooges.add( "Curly" );
>       }
>       public List getStooges() {
>         return new ArrayList( stooges );
>       }
> }

See JLS ยง17.5:
"The usage model for final fields is a simple one. Set the final fields for an object in that object's constructor. Do not write a reference to the object being constructed in a place where another thread can see it before the object's constructor is finished. If this is followed, then when the object is seen by another thread, that thread will always see the correctly constructed version of that object's final fields. It will also see versions of any object or array referenced by those final fields that are at least as up-to-date as the final fields are."

-- 
Lew

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