[concurrency-interest] concurrency puzzle

David Holmes dcholmes at optusnet.com.au
Sun Sep 10 21:45:28 EDT 2006


yangjs writes:
> >>    public void foo(){
> >>       x = 20;
> >>       synchronized(this){
> >>          System.out.println( x );
> >>       }
> >>    }
> >> }
> .According to JLS ,
>     "Each action in a thread happens-before every action in that
> thread that comes later in the program order".

Yes

>   so the println out only have one result :20

Not necessarily. If the instance was constructed in a different thread then
the write of x=10 from the constructor can occur after the x=20 and before
the println(x), hence the output can be 10 or 20.

David Holmes



More information about the Concurrency-interest mailing list