visibility on array elements

N

neuneudr

Hi,

imagine that for whatever reason I decide *not* to use an
AtomicLongArray and go with a long[] (the reason is
actually that I want to understand what's going on).

public static final long[] ref = new long[42];

Do the following guarantee the correct visibility
when accessing the elements of the array :

From one thread :

synchronized (ref) {
ref[0] = 3;
}

and from another thread, later :

synchronized (ref) {
long val = ref[0]; // Am I guaranteed to get back '3'
here ?
}


Basically my question is : seen that array elements cannot
be declared volatile *and* that I want to use arrays, how
should I proceed if I want thread-safe behavior when
modifying/reading my array ?
 
A

Arne Vajhøj

imagine that for whatever reason I decide *not* to use an
AtomicLongArray and go with a long[] (the reason is
actually that I want to understand what's going on).

public static final long[] ref = new long[42];

Do the following guarantee the correct visibility
when accessing the elements of the array :

From one thread :

synchronized (ref) {
ref[0] = 3;
}

and from another thread, later :

synchronized (ref) {
long val = ref[0]; // Am I guaranteed to get back '3'
here ?
}

Yes.

As I understand the Java memory model.
Basically my question is : seen that array elements cannot
be declared volatile *and* that I want to use arrays, how
should I proceed if I want thread-safe behavior when
modifying/reading my array ?

If you use Java 1.5 or newer then volatile should work, because
writing and reading a volatile (the array ref) should make
other variable (array elements) visible.

As I understand the Java memory model.

Arne
 
E

Eric Sosman

Hi,

imagine that for whatever reason I decide *not* to use an
AtomicLongArray and go with a long[] (the reason is
actually that I want to understand what's going on).

public static final long[] ref = new long[42];

Do the following guarantee the correct visibility
when accessing the elements of the array :

From one thread :

synchronized (ref) {
ref[0] = 3;
}

and from another thread, later :

synchronized (ref) {
long val = ref[0]; // Am I guaranteed to get back '3'
here ?
}

Yes, you'll get 3L (assuming nothing else has disturbed
ref[0] in the meantime).
Basically my question is : seen that array elements cannot
be declared volatile *and* that I want to use arrays, how
should I proceed if I want thread-safe behavior when
modifying/reading my array ?

You can synchronize all accesses on the array itself, as
above, in which case no accesses will overlap and modifications
made by any thread will be visible to all others. Note that
this may create more contention than if you could somehow
synchronize on the individual elements, because it prohibits
simultaneous access to ref[0] and ref[1].

Using synchronized access (or volatile) does not in and of
itself guarantee "thread-safe behavior." A classic example is

Vector<String> v = new Vector<String>();
...
if (! v.isEmpty()) {
String s = v.remove(0);
...

This fragment is not thread-safe, even though the individual
..isEmpty() and .remove() methods are synchronized and hence
thread-safe insofar as their own operations are concerned.
 
N

neuneudr

Thanks a lot to you and Arne (and the others that will jump in)
for answering my question,

Driss
 
L

Lew

public static final long[] ref = new long[42];

Do the following guarantee the correct visibility
when accessing the elements of the array :

From one thread :

synchronized (ref) {
ref[0] = 3;
}

and from another thread, later :

synchronized (ref) {
long val = ref[0]; // Am I guaranteed to get back '3'
here ?
}

Provided no thread intervenes with another write to 'ref[0]' before the read,
with a /happens-before/ relationship to the read and a /happens-after/
relationship to the stated write, the read to 'val' will "see" 3 as the value.

(Just to make that assumption explicit.)

<http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.4.5>
<http://www.ibm.com/developerworks/library/j-jtp03304/>
<http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top