[concurrency-interest] concurrency puzzle

yangjs yangjs at alibaba-inc.com
Sun Sep 10 21:41:31 EDT 2006


 >
> Some confusion seems to have arisen.
>
>>    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".

  so the println out only have one result :20






Best regards,


 ???(yangjs)
Tel:0571-85022088-3021
Email:yangjs at alibaba-inc.com
http://www.alibaba.com
----- Original Message ----- 
From: "David Holmes" <dcholmes at optusnet.com.au>
To: "Peter Veentjer" <alarmnummer at gmail.com>; 
<concurrency-interest at cs.oswego.edu>
Sent: Monday, September 11, 2006 7:22 AM
Subject: Re: [concurrency-interest] concurrency puzzle


> Peter,
>
> Some confusion seems to have arisen.
>
>>    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)
>
> In the absence of any other methods that write to x then the value of x is
> always 20 when the println is invoked. It is irrelevant whether the write 
> to
> x is visible to some other thread at that time as no other thread has 
> access
> to x. Any thread that executes foo() will do the assignment x=20 and then
> print out the value 20 because in program order the write to x
> happens-before the read of x used by the println.
>
> It would be a different matter if there was a seperate setter method that
> could be called. I think you need to reconsider your example.
>
> Cheers,
> David Holmes
>
> _______________________________________________
> 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