lgp
lgp

Reputation: 335

java - stale reference to atomicintegerarray possible?

What happens when I assign a new instance of AtomicIntegerArray to a variable in a multithreaded environment?

AtomicIntegerArray array = new AtomicIntegerArray(...);
do some stuff
array = new AtomicIntegerArray(...);

Might some threads still access the stale reference to the old instance after the new assignment? If so, would I need to declare the atomic array as volatile as well?

Upvotes: 0

Views: 250

Answers (2)

jtahlborn
jtahlborn

Reputation: 53694

if array is visible to other threads (e.g. a class member variable) then yes, it would need to be volatile as well.

Upvotes: 2

parsifal
parsifal

Reputation: 1527

Your array variable appears to be a local variable, which by definition cannot be accessed by multiple threads.

However, if it's really an instance variable, then yes, other threads can see stale values, just like they can with any other instance variable. What an instance variable references does not affect how that instance variable is accessed.

Upvotes: 0

Related Questions