Re: JavaMemoryModel: Thread starting in constructors

From: Jeremy Manson (jmanson@cs.umd.edu)
Date: Tue Mar 12 2002 - 10:54:17 EST


>
> How about:
>
> f) the thread will be SUSPENDED and RESUMED automatically
> on exit from the most-derived class constructor (exceptions
> aside);

That doesn't solve the problem in terms of the semantics. There is no
implicit synchronzation associated with Thread.suspend() and
Thread.resume() (nor should there be, I suspect; it's good to stay away
from those methods). Even if you were to use wait/notify, it isn't a good
idea because the constructor may never end (unless you mean that this only
applies to threads started on the last line of the constructor), viz.:

class A {
         int x;
         final int y;
         Object lock = new Object();
         public A(int a, int b) {
                 x = a;
                 y = b;
                 new Thread() {
                     public void run() {
                         System.out.println("x = " + x + ", y = " + y);
                         }}.start();
                 }
                while (true) {
                }
         }

More realistically, there could be a wait in the constructor dependent on
a notify in the thread. This is left as an exercise for the reader.

> or, even better:
>
> g) a rather simple mechanism of POST-constructors could be
> introduced to SAFELY/MEANINGFULLY exploit the polymorphic
> behavior (publish this, start threads, etc) at "construction"
> time without all those silly "manual" init()s/factories/final
> wrappers, etc.

That would be nice, but it wouldn't help existing code. That probably
needs its own JSR, too.

                                        Jeremy
-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:38 EDT