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


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

Synchronization of the constructor

From MaciekL <__nospam__maclab@o2.pl>
Newsgroups comp.lang.java.programmer
Subject Synchronization of the constructor
Date 2011-08-13 11:58 +0200
Organization http://onet.pl
Message-ID <j25hro$cjf$1@news.onet.pl> (permalink)

Show all headers | 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 | NextNext 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