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: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Synchronization of the constructor Date: Sat, 13 Aug 2011 09:36:49 -0400 Organization: A noiseless patient Spider Lines: 62 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 13 Aug 2011 13:40:02 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="f8igmItKsWs6nM5YanFxAA"; logging-data="1014"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+oVcOT0WvCA0yvHx6I07Ou" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: Cancel-Lock: sha1:83FKb/6Z4YfbVReliaq7APvtdis= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7079 On 8/13/2011 5:58 AM, MaciekL wrote: > Hi, > > I have a doubt because Java disables synchronization of the constructor > by default. The constructor can be synchronized, but not on the object being constructed. The `this' object is not a full-fledged instance until the constructor finishes -- more generally, until all its constructors finish, including those of subclasses. While the instance is in an incomplete and fragile state, before it's completely constructed, you must be careful not to let it be "seen" outside its chain of constructors. Java forbids the bare `synchronized' on a constructor because the object you want to synchronize on doesn't truly exist yet. If some other thread can "see" the unformed object, that's an error in your code -- and if no other thread can "see" it, it needs no synchronization anyhow. > [...] > I have found a soultion, adding whole definition of the Constructor into > synchronized block. > ............... > synchronized (this) > { > ... // ConstructorBody > } That's not a solution; it just hides your error so the Java compiler doesn't notice it. The error message disappears, but the error itself remains. > public class TestApp implements Runnable > { > Object obj = null; > public static void info(String s) > { > System.err.println("[" + Thread.currentThread().toString() + "] " + s); > } > public TestApp(int timeout) > { > info(" {TestApp(" + timeout + ")} Begin"); > Thread runner = (new Thread(this)); ... and here's the error. The `this' object is still incomplete, not fully able to carry out its duties as a TestApp instance. Yet you have given it to the new Thread to play with, and the Thread expects `this' to be able to function as a full-fledged TestApp. The `this' object is still in the womb, but the Thread needs it to behave as a full-grown adult. Don't Do That. Always let the constructor(s) run to completion before revealing the new instance to the rest of the program. Don't give a `this' reference to another thread, or put it in a globally- accessible array or collection, or even call a class method that may have been overridden in a subclass. Until they reach maturity, you must not ask children to assume adult responsibilities. -- Eric Sosman esosman@ieee-dot-org.invalid