Thinking about the poor programmer is a good idea. I don't know that
requiring synchronization for reading data created by the starting thread is
adequate, even though it seems consistent with other visibility rules.
If nothing else, when creating threads in Java, I usually create a Runnable,
create a Thread passing it the Runnable, and then tell the thread to start.
At the very least I want to know that the entire Runnable is visible to the
thread that has just been spawned.
Further, when I create the Runnable, I might either create additional
objects I want that Runnable to use, or give that Runnable access to some
other objects that have been sitting around. It seems reasonable that all of
this Runnable initialization should be visible in the new thread.
Since the Runnable is passed into the Thread constructor, and since there
are no methods for adding or changing the Runnable a Thread is associated
with after the Thread is created, it sounds like the minimum guarantee
should be that the first write in the example should be visible. Visibility
of the first write corresponds to visibility of a Runnable that has been
created and initialized before the Thread was created.
I agree with Allen that there is no intuitive reason for any stronger
guarantee than this. I disagree with the suggestion that synchronization
could be used for all viewing of the spawning threads data. Whether or not
the visibility of the first write is intuitive, without it there is no way
to initialize a Runnable and know that the started thread will be able to
see all the data written to the Runnable.
> -----Original Message-----
> From: Allen Holub [mailto:email@example.com]
> Sent: Tuesday, June 13, 2000 4:30 PM
> To: Paul Jakubik; 'Bill Pugh'; javaMemoryModel@cs.umd.edu
> Subject: RE: JavaMemoryModel: Question about _current_ semantics of
> Throwing in my two cents. I think it's instructive to think
> about the poor programmer rather then the underlying OS. I'm
> not sure that either behavior is particularly intuitive. It
> seems reasonable to assume that synchronization must be used
> if the created thread wants to access the memory of the
> creating thread. Requiring the writes between the create and
> start to be visible is, in any event, inconsistent with all
> other visibility rules, all of which require synchronized or
> volatile. Moreover, it's at times convenient for all
> thread-creation overhead to be buried in start() rather than
> the constructor. This way you can create an array of the
> things and incur no overhead until the array elements are
> actually started.
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel
This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:26 EDT