Re: JavaMemoryModel: Thread starting in constructors

From: Alexander Terekhov (TEREKHOV@de.ibm.com)
Date: Tue Mar 12 2002 - 09:16:43 EST


> OK, this is just a simple example of something I'm trying to fine tune:
>
> class A {
> int x;
> final int y;
> public A(int a, int b) {
> x = a;
> y = b;
> new Thread() {
> public void run() {
> System.out.println("x = " + x + ", y = " + y);
> }}.start();
> }
> }
>
> Assume someone invokes new A(1,2). Which of the might occur?
>
> a) prints x = 1, y = 2
> b) prints x = 0, y = 0
> c) prints x = 0, y = 2
> d) prints x = 1, y = 0
> e) the thread started in the constructor throws a NullPointerException

How about:

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

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.

;-)

regards,
alexander.

-------------------------------
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