[concurrency-interest] concurrency puzzle

Peter Veentjer alarmnummer at gmail.com
Tue Sep 12 14:15:45 EDT 2006


I know 10 could be printed if the bar method is executed just after
a=20 and just before the synchronized(this){println(a)}. But what if
bar is executed some time before the foo method is called? Could 10
still be printed?

On 9/12/06, Peter Veentjer <alarmnummer at gmail.com> wrote:
> I'm stilling thinking about the subject and I trying to think in the
> happens before terms.
>
> Another example with a happens before relation
>
> private int a;
>
> public void foo(){
>     a = 20;
>     synchronized(this){
>         System.out.println(a);
>     }
> }
>
> public void bar(){
>     synchronized(this){
>         a=10;
>     }
> }
>
>
> In this example, could it happen that 10 is printed?
>
> There is a happens before relation between x=10 and System.out.println
> (when the lock is released at the end of the bar method, and acquired
> in the foo method.
>


More information about the Concurrency-interest mailing list