[concurrency-interest] Volatile array and Wait

Shaffer, Darron Darron_Shaffer at stercomm.com
Wed Oct 19 12:07:41 EDT 2005

The second option can use the volatile reference to the array as the

So given:

  volatile Object[] b = new Object[50];

reading looks like this:

   Object x = b[25];  // Volatile read must occur first, to find the

writing looks like this:

   b[22] = x; Object dummy = b;  // Must have trailing volatile read.

If you do this, be sure and comment it well enough that a
junior/maintenance programmer doesn't decide that volatile applies to
the entire array.

-----Original Message-----
From: concurrency-interest-bounces at cs.oswego.edu
[mailto:concurrency-interest-bounces at cs.oswego.edu] On Behalf Of Jeremy
Sent: Wednesday, October 19, 2005 9:40 AM
To: Inanc Gumus
Cc: Vijay; concurrency-interest at altair.cs.oswego.edu
Subject: Re: [concurrency-interest] Volatile array and Wait

Inanc Gumus wrote:
> So, how to make inter array elements volatile?

There is no way to do this directly.  The simplest option is probably to

use AtomicReferenceArray, AtomicIntegerArray or AtomicLongArray.

Another option is to have an extra volatile dummy field associated with 
the array.  After each write to an array element, write to the dummy 
field (it doesn't matter what value is written).  Before each read of an

array element, read the dummy field (and discard the value).

A third option is to use indirection.  That is to say, you could create 
an array of references to:

      class VolatileRef {
        public volatile Object ref = null;

A fourth option is to use locking instead.

Concurrency-interest mailing list
Concurrency-interest at altair.cs.oswego.edu

More information about the Concurrency-interest mailing list