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


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

Re: Synchronization of the constructor

From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Synchronization of the constructor
Date 2011-08-13 09:23 -0700
Organization http://groups.google.com
Message-ID <fb83dde2-63a4-4990-b8f9-af413301c31d@glegroupsg2000goo.googlegroups.com> (permalink)
References <j25hro$cjf$1@news.onet.pl> <j25uri$vm$1@dont-email.me> <j264b6$3u5$1@dont-email.me>

Show all headers | View raw


markspace wrote:
> Eric Sosman wrote:
>> MaciekL wrote:
>>> I have a doubt because Java disables synchronization of the constructor
>>> by default.
>>
>> The constructor can be synchronized,
> 
> It really can't.  Recall that the compiler will insert a call to a super 
> constructor if the first statement doesn't have a such a call or a call 
> to another class constructor.  Consider this:
> 
> public class SomeClass {
>    public SomeClass() {
>      synchronized( SomeClass.class ) {
>        ...
>      }
>    }
> ...
> 
> What you get is this:
> 
> public class SomeClass {
>    public SomeClass() {
>      super();
>      synchronized( SomeClass.class ) {
>        ...
>      }
>    }
> ...
> 
> And thus you see that some writes in the construction occur outside of 
> the synchronized block, a classic case of incorrectly written 
> synchronization.
> 
> The closest Java gets to a synchronized constructor is immutable objects 
> made with final fields.
> 
> public class Immutable {
>    private final SomeObject o;
>    public Immutable() {
>      o = new SomeObject();
>    }
> ...
> 
> This is thread safe, and immutable, because the fields written are 
> declared "final."  Java takes special processing at the end of the 
> constructor to synchronize all final fields, and any writes made to 
> objects accessible via those final fields, with all other threads in the 
> system.  So now this Immutable class can be used safely by any thread in 
> the system.

Others have explained your error quite well, OP, so I will merely add that you should read the Java Language Specification on threads, synchronization and the /happens-before/ relationship.  Also, read and study /Effective Java/ by Joshua Bloch, /Java Concurrency in Practice/ by Brian Goetz, et al., the book by Doug Lea whose title escapes me just now (lmgtfy?) and any article in the IBM Developerworks for Java site's series, especially those by Goetz, Bloch and others on the topic of threaded Java code.

Bottom line:  constructors are wholly for _construction_, only construction and nothing but construction, not operations on the object.  Do your logic outside the constructor, after the constructor's promises have a chance to be kept.

-- 
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