Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx01.iad01.newshosting.com!newshosting.com!newsfeed.neostrada.pl!unt-exc-02.news.neostrada.pl!news.onet.pl!.POSTED!not-for-mail From: MaciekL <__nospam__maclab@o2.pl> Newsgroups: comp.lang.java.programmer Subject: Synchronization of the constructor Date: Sat, 13 Aug 2011 11:58:15 +0200 Organization: http://onet.pl Lines: 84 Message-ID: NNTP-Posting-Host: 62.29.174.65 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: news.onet.pl 1313229496 12911 62.29.174.65 (13 Aug 2011 09:58:16 GMT) X-Complaints-To: niusy@onet.pl NNTP-Posting-Date: Sat, 13 Aug 2011 09:58:16 +0000 (UTC) User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20110624 Thunderbird/5.0 X-Enigmail-Version: 1.2 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7072 Hi, I have a doubt because Java disables synchronization of the constructor by default. Following example shows that lack of synchronization makes NullPointerException. ................ [Thread[main,5,main]] {TestApp(0)} Begin [Thread[runner,5,main]] Begin [Thread[runner,5,main]] Test Exception in thread "runner" java.lang.NullPointerException at TestApp.test(TestApp.java:61) at TestApp.run(TestApp.java:55) at java.lang.Thread.run(Thread.java:662) [Thread[main,5,main]] Test [Thread[main,5,main]] OBJ = java.lang.Object@9304b1 [Thread[main,5,main]] {TestApp(0)} End ................ Synchronized 'test' method may be called by another thread in case the Object is not created. This undefined behaviour is very tricky, note that following example may works correctly, it depends on the Constructor duration time. I have found a soultion, adding whole definition of the Constructor into synchronized block. ............... synchronized (this) { ... // ConstructorBody } ............... I don't understand why Java forbids simple declaration: ............... public synchronized TestApp(int timeout) ............... Because of that all calls to "add*Listener(this)" from constructor should be carrefully implemented. /*--:BEG:--[TestApp.java]----------------------------------------------------*/ 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)); runner.setName("runner"); runner.start(); try { Thread.sleep(timeout); } catch (InterruptedException ie) { } obj = new Object(); test(); info(" {TestApp(" + timeout + ")} End"); } public void run() { info("Begin"); test(); info("End"); } public synchronized void test() { info("Test"); info("OBJ = " + obj.toString()); } public static void main(String [] args) { new TestApp(1000); } } /*--:EOF:--[TestApp.java]----------------------------------------------------*/ Regards -- Maciek