Re: JavaMemoryModel: Thread starting in constructors

From: Joshua Bloch (joshua.bloch@sun.com)
Date: Mon Mar 11 2002 - 22:57:32 EST


Bill,

> 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

   Clearly I'm being dense here, but why is anything other than (a) legal?
I always thought that there was implicit synchronization between a thread
that started a thread and the thread that it started. If anything besides
(a) is legal, people will be mightily confused.

          Josh

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