Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!news-out.readnews.com!transit3.readnews.com!postnews.google.com!y39g2000prd.googlegroups.com!not-for-mail From: kedar mhaswade Newsgroups: comp.lang.java.programmer Subject: Re: Synchronization of the constructor Date: Sat, 13 Aug 2011 23:12:56 -0700 (PDT) Organization: http://groups.google.com Lines: 63 Message-ID: References: NNTP-Posting-Host: 208.127.246.23 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1313302376 22300 127.0.0.1 (14 Aug 2011 06:12:56 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 14 Aug 2011 06:12:56 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: y39g2000prd.googlegroups.com; posting-host=208.127.246.23; posting-account=lZExVgoAAAC6LNhvTOESNqEOAjRzDXhY User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HNKUARELSC X-HTTP-UserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 (KHTML, like Gecko) Ubuntu/10.10 Chromium/15.0.836.0 Chrome/15.0.836.0 Safari/535.1,gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7101 On Aug 13, 8:13=A0am, markspace <-@.> wrote: > On 8/13/2011 6:36 AM, Eric Sosman wrote: > > > On 8/13/2011 5:58 AM, MaciekL wrote: > >> Hi, > > >> I have a doubt because Java disables synchronization of the constructo= r > >> by default. > > > The constructor can be synchronized, > > It really can't. =A0Recall that the compiler will insert a call to a supe= r > constructor if the first statement doesn't have a such a call or a call > to another class constructor. =A0Consider this: > > public class SomeClass { > =A0 =A0public SomeClass() { > =A0 =A0 =A0synchronized( SomeClass.class ) { > =A0 =A0 =A0 =A0... > =A0 =A0 =A0} > =A0 =A0} > ... > > What you get is this: > > public class SomeClass { > =A0 =A0public SomeClass() { > =A0 =A0 =A0super(); > =A0 =A0 =A0synchronized( SomeClass.class ) { > =A0 =A0 =A0 =A0... > =A0 =A0 =A0} > =A0 =A0} > ... > > 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 { > =A0 =A0private final SomeObject o; > =A0 =A0public Immutable() { > =A0 =A0 =A0o =3D new SomeObject(); > =A0 =A0} > ... > > This is thread safe, and immutable, because the fields written are > declared "final." =A0Java 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. =A0So now this Immutable class can be used safely by any thread i= n > the system. Doesn't that depend on how Immutable SomeObject is and how Immutable's immutability relates to it? Also, other standard caveats apply (e.g. Immutable should be declared final).