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


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

Re: Synchronization of the constructor

From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Synchronization of the constructor
Date 2011-08-13 09:36 -0400
Organization A noiseless patient Spider
Message-ID <j25uri$vm$1@dont-email.me> (permalink)
References <j25hro$cjf$1@news.onet.pl>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next 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