[concurrency-interest] Exposing self-references in constructors

David Holmes dcholmes at optusnet.com.au
Wed Dec 20 18:41:29 EST 2006


When you say thread B is "later executed" do you mean start() is called
later? and by the thread that constructed A? There is a happens-before
relationship between the start() of a thread and the first action of that
thread.

Otherwise it all depends on the exact call sequence and what threads are
involved.

There are a number of implicit synchronization actions that occur when
creating and starting threads that could impose the right happens-before
relationships - eg. synchronization when adding to the thread group.

Cheers,
David Holmes
  -----Original Message-----
  From: concurrency-interest-bounces at cs.oswego.edu
[mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Ryan
LeCompte
  Sent: Thursday, 21 December 2006 8:44 AM
  To: concurrency-interest at cs.oswego.edu
  Subject: [concurrency-interest] Exposing self-references in constructors


  Hello all,

  Here is a scenario:

  - In Thread A's constructor, a reference to "this" is passed along which
ultimately ends up stored as a reference in Thread B.
  - Thread A's constructor completes and is fully instantiated.
  - Thread B then is later executed (let's say restrained at 30 seconds
later) -- and then uses the reference to Thread A that it was given in
Thread A's constructor.

  Is it possible that Thread B's reference to Thread A could be "stale" ?
I'm not seeing that this is the case. I know that it's bad practice to hand
off references to yourself before you are fully constructed, but in this
paticular case I'd like to know if I'm "safe" if I have external time
constraints that force Thread B to not read its reference to Thread A until
a later time when Thread A has finished fully instantiating.

  Also, what if Thread B is already executing and just operates on a an
object instance that contains the reference stored in Thread A's
constructor? Consider a scheduling framework where a  pool of threads are
already instantiated and running, and simply periodically execute "work
instances" that contain the logic to be executed.

  Thoughts?

  Thanks
  Ryan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20061221/c8e0e829/attachment-0001.html 


More information about the Concurrency-interest mailing list