[concurrency-interest] concurrency puzzle

Peter Veentjer alarmnummer at gmail.com
Sun Sep 10 14:18:45 EDT 2006


On 9/10/06, Jeremy Manson <jmanson at cs.umd.edu> wrote:
> I'm not sure what you mean by multithreaded environment, because all
> Java code runs in a multithreaded environment.

True. But I mean that the object is used by multiple threads.

> Then it will either print 20 or 10.  It can't print 0 because the write
> of the default value (conceptually) happens-before the first action of
> every thread.
I was not sure about this one. I thought the following would happen
1) a stackframe is allocated with all defaults for the fields (x=0)
2) the initilization for the fields is called (so the x=10)
3) constructor call

I have been looking through the documentation, but the initialisation
(x=10) felt the same as a constructor.

So there is a difference between:

public class A{
    private int a;
    public A(){
       a = 10;
    }
    ...
}

and

public class A{
    private int a = 10;
    ...
}

Is there a difference?

> This can be avoided by making global.a volatile, although you still
> might get a NullPointerException in Thread 2.
I know, but I'm trying to understand what is allowed by the JMM.

> I'm not sure which JMM documentation you have been examining, if it
> talks about local memory and invalidation.  The old one had kind-of a
> notion of local memory, but it hasn't been the "active" JMM for a good
> while.  Certainly, that JMM didn't talk about safe publication.
JSR133, JCIP and Concurrent and Realtime programming in Java.

> And speaking of safe publication - the write of 10 to x happens in the
> constructor, according to the semantics of the Java programming
> language.  If it were a final field (which is where the notion of safe
> publication is important), then it would be considered to be published
> safely, unless a reference to the object escapes an otherwise-unseen
> constructor.
Instruction reordering makes it possible to see a partially constructed object

reg = alloc(A)
reg.x=10
global.a = reg

This could be reordened to:

reg = alloc(A)
global.a = reg
reg.x=10

If another thread picks up a and calls foo before the ref.x=10 is
called, the println could see a zero.

> It can avoid this by synchronizing properly.
Ofcourse... but I'm trying to understand what can happen if programs
are not synchronized propertly. I want to understand how the JMM works
and that is why I made up this example.

>
>                                         Jeremy

Thanks for your reply.

>
> Peter Veentjer wrote:
> > I'm playing with the Java Memory Model documentation. And to check if
> > my understanding is correct, I'm interested in the following example:
> >
> > public class A{
> >    private int x = 10;
> >
> >    public void foo(){
> >       x = 20;
> >       synchronized(this){
> >          System.out.println( x );
> >       }
> >    }
> > }
> >
> > If this class is used in a multithreaded environment, what could the
> > output be? (foo is called only once to make it easy)
> >
> > 20: if the x=20 is written to main memory, the read for println( x )
> > will return 20.
> >
> > 10: if x=20 isn't written to main memory, but only in local memory,
> > the write (x=20) could get lost. This is because the local memory is
> > invalidated when the lock on this is acquired. When x is read for the
> > println, it will return the value in main memory (and that is 10).
> >
> > 0: the 10 value doesn't need to be visible in main memory because it
> > isn't published safely. So for the same reasons 10 could be read for
> > the println, the default value for int (that is a 0) could be read for
> > the println.
> >
> > 0: a different thread could obtain a partially initialized reference
> > to A because of reordening. So 0 could be the value of x when it is
> > read for the println statement. So this is another way the 0 value
> > could be found.
> >
> > So my bet would be
> >
> > 20 or 10 or 0.
> >
> > Is my understanding correct?
> > _______________________________________________
> > Concurrency-interest mailing list
> > Concurrency-interest at altair.cs.oswego.edu
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>


More information about the Concurrency-interest mailing list