David Smiley <dsmiley@mitre.org> writes:
> (2) Copy on write. Again, I assume that another thread
> will /eventually/ see the new reference to whatever was
> replaced in a reasonably small amount of time even
> though there is no explicit memory barrier.
You mean something like the following?
Foo foo = new Foo();
Singleton.FOO = foo;
This situation is like your first example (simple boolean assignment), in
that the compiler might outsmart you and hide the new foo from other
threads.
A nastier problem is illustrated by the following example:
int[] cell = { 1 };
Singleton.CELL = cell;
Because writes may appear out of order, the reading thread may see the
reference to the new cell before its state has been initialized. In other
words, the reading thread may see cell[0]==0.
The Foo example may also be vulnerable to this 2nd problem unless Foo itself
is thread-safe. There has been some talk about granting special properties
to the constructor to guard against this, but I'm not sure where that stands
(Bill?).
-- Joe Bowbeer------------------------------- JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel
This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:30 EDT