Re: JavaMemoryModel: Final and Volatile array classes

From: Bill Pugh (pugh@cs.umd.edu)
Date: Thu Mar 23 2000 - 16:03:28 EST


At 3:50 PM -0500 3/23/00, Doug Lea wrote:
> For example, in contradiction to V4,
>suppose I want to grab a volatile ref field and hold it in a local to
>avoid multiple barriers while using it to access multiple fields? In
>the absence of ways to express these things, do I just live with the
>fact that I cannot do them?
>
>-Doug

Say we have

class Foo {
        static volatile int [] a = new int[100];
        }

If I use the code:

for(int i = 0; i <Foo.a.length; i++)
        total += Foo.a[i];
Then the code will perform 201 reads of the volatile field a,
potentially with memory barrier(s) required for each (depending
on the architecture).

Alternatively, if I use the code:

int [] tmp = Foo.a;
for(int i = 0; i < tmp.length; i++)
        total += tmp[i]

Then I perform only one read of a volatile and only need one or two
memory barriers.

        Bill
-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:24 EDT