Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7072
| 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 | <j25hro$cjf$1@news.onet.pl> (permalink) |
| 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 |
Show key headers only | View raw
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
Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar
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