[concurrency-interest] Volatile array and Wait

David Holmes dholmes at dltech.com.au
Wed Oct 19 12:35:44 EDT 2005

> Shaffer, Darron writes:
> The second option can use the volatile reference to the array as the
> "dummy".
> So given:
>   volatile Object[] b = new Object[50];
> reading looks like this:
>    Object x = b[25];  // Volatile read must occur first, to find the
> array.
> writing looks like this:
>    b[22] = x; Object dummy = b;  // Must have trailing volatile read.

No you need a volatile *write* to happen-before the volatile read. So you
would need to do:

     b[22] = x;  // write normal data
     b = b;      // volatile write

but this is not sufficient to guarantee that an element read sees the most
recent value, as the read could occur after the write to the element but
before the write to the array reference. Of course the program can't
distinguish this case from the case where the read of the element happens
before the write of the element.

David Holmes

More information about the Concurrency-interest mailing list