>Dennis Sosnoski wrote:
> >
> > Bill Pugh wrote:
> > >
> > > - Provide a synchronization idiom that provides a safe way for a thread
> > > to construct an object, perform a series of updates on the object,
> > > and then publish the object (store a reference to the
>object in memory
> > > read by other threads). Other threads should then be able
>to read that
> > > reference and see an object that reflects all of the modifications.
> > > Importantly, the threads reading that reference should be
>able to do so
> > > without any synchronization.
> > >
> >
> > The flush operation would also do this. However, can't this be
>done now with
> > multiple synchronize operations? I believe the following code works:
> >
> > public Foo fooRef;
> >
> > public void switchFoo() {
> > Foo foo = new Foo();
> > foo.mutate();
> > synchronized(foo){} // force update of foo in memory
> > synchronized(foo) { // prevent reordering of reference write
> > fooRef = foo;
> > }
> > }
> >
> > - Dennis
This isn't guaranteed by the existing JMM. You would expect that is
the modifications to foo
are forced to main memory before the store into fooRef is allowed to
go to main memory,
you would think that you would be safe.
But that doesn't work in the current model. If a thread reads fooRef
and sees the new value, and then reads a field of fooRef, the value
seen for the field of fooRef is allowed to be a stale value: one
that was in main memory before the fooRef was updated. (This is
assuming reading thread does no synchronization).
Bill Pugh
-------------------------------
This is the JavaMemoryModel mailing list, managed by Majordomo 1.94.4.
To send a message to the list, email JavaMemoryModel@cs.umd.edu
To send a request to the list, email majordomo@cs.umd.edu and put
your request in the body of the message (use the request "help" for help).
For more information, visit http://www.cs.umd.edu/~pugh/java/memoryModel
This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:12 EDT