Questions tagged [atomicreference]

Java atomic.AtomicReference. (Use [stdatomic] for questions about C++20 std::atomic_ref)

Java

java.util.concurrent.atomic.AtomicReference<V> was new in Java 1.5

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/atomic/AtomicReference.html

C++ - use for C++ atomic_ref<T>

std::atomic_ref<T> was new in C++20, allowing atomic ops on properly-aligned plain objects. Such as alignas(std::atomic_ref<long>::required_alignment) long foo;

https://en.cppreference.com/w/cpp/atomic/atomic_ref

55 questions
433
votes
7 answers

When to use AtomicReference in Java?

When do we use AtomicReference? Is it needed to create objects in all multithreaded programs? Provide a simple example where AtomicReference should be used.
Chintu
  • 4,333
  • 3
  • 15
  • 4
29
votes
3 answers

AtomicReferenceFieldUpdater - methods set, get, compareAndSet semantics

From the Java AtomicReferenceFieldUpdater docs: Note that the guarantees of the compareAndSet method in this class are weaker than in other atomic classes. Because this class cannot ensure that all uses of the field are appropriate for purposes…
axel22
  • 32,045
  • 9
  • 125
  • 137
23
votes
4 answers

what's the difference between compareAndSet and weakCompareAndSet in AtomicReference?

The source code is the same thing. public final boolean compareAndSet(V expect, V update) { return unsafe.compareAndSwapObject(this, valueOffset, expect, update); } public final boolean weakCompareAndSet(V expect, V update) { return…
Eric
  • 241
  • 2
  • 5
13
votes
2 answers

Possible to safely increment BigInteger in a thread safe way, perhaps with AtomicReference, w/o locking?

A lot of our code is legacy but we are moving to a "Big Data" back-end and I'm trying to evangelize the newer API calls, encourage the use of the latest Spring libraries etc. One of our problems is application layer ID generation. For reasons I…
user447607
  • 5,149
  • 13
  • 33
  • 55
9
votes
4 answers

AtomicReference to a mutable object and visibility

Say I have an AtomicReferenceto a list of objects: AtomicReference> batch = new AtomicReference>(new ArrayList()); Thread A adds elements to this list: batch.get().add(o); Later, thread B takes the list and, for…
Jan Van den bosch
  • 3,542
  • 3
  • 26
  • 38
9
votes
4 answers

What´s the difference between AtomicReference vs. AtomicInteger?

I don´t get the difference between these two: AtomicReference atomicReference = new AtomicReference<>(1); vs. AtomicInteger atomicInteger = new AtomicInteger(1); Can someone generally say when to use AtomicReference? Hope someone can help…
GedankenNebel
  • 2,437
  • 5
  • 29
  • 39
7
votes
2 answers

Possible to create AtomicReference that can be swapped atomically?

Is there any way to implement a type of reference whose value can be exchanged with another atomically? In Java we have AtomicReference which can be swapped with a local variable but not with another AtomicReference. You can do: AtomicReference r1…
finnw
  • 47,861
  • 24
  • 143
  • 221
5
votes
0 answers

Is it appropriate to use AtomicReference with a java lambda?

I often find myself using this idiom: AtomicReference coolReference = new AtomicReference(new MyCoolObject()); getOptionalValue().ifPresent(presentValue -> { coolReference.set(new MyCoolObject(presentValue)); }); return…
afspear
  • 51
  • 1
  • 3
5
votes
5 answers

AtomicReference in Java - necessary for setting a reference in a thread-safe environment?

In Java there exists an AtomicReference class. Does this mean that setting a reference is NOT an atomic operation in and of itself? e.g., is this not thread-safe (assuming that the value returned cannot be modified)?: public void someMethod() { …
Cornelius
  • 71
  • 1
  • 4
5
votes
4 answers

multiple fields: volatile or AtomicReference?

I have to synchronize access between threads to a shared object, whose state consists of several fields. Say: class Shared{ String a; Integer b; //constructor, getters and setters .... } I have possibly many threads reading this objects,…
legrass
  • 407
  • 5
  • 13
5
votes
1 answer

GCC atomic builtins and volatile

I use some global structures in a multithreaded program, some of the members are modified by multiple threads simultaneously, some others are not. I didn't define any of this members volatile, but anytime i use this members for both reading and…
Kurt
  • 53
  • 1
  • 7
4
votes
4 answers

Please explain final AtomicReference

can someone explain me this: final AtomicReference atomicReference = new AtomicReference<>(1); atomicReference.set(2); In what sense is final used?
GedankenNebel
  • 2,437
  • 5
  • 29
  • 39
3
votes
1 answer

What is a usecase for Java AtomicReference#getAndSet?

What is a usecase for Java AtomicReference#getAndSet? In other words, is it correct assumption, that if the only method from AtomicReference that I use in my code is AtomicReference#getAndSet, then I do not need AtomicReference at all, just a…
yaskovdev
  • 1,213
  • 1
  • 12
  • 22
3
votes
2 answers

2 threads performing myAtomicReference.compareAndSet(expected,new Date())

static boolean unsynchronizedSetter(Date expected){ Date newDate = new Date(); AtomicReference myAtomicReference = Lookup.getAtomicRef(); boolean myStatus = myAtomicReference.compareAndSet(expected, newDate); //CAS return…
tanbin
  • 31
  • 2
3
votes
2 answers

Are Mutable Atomic References a Bad Idea?

I have a data structure that I occasionally wish to modify, and occasionally wish to replace outright. At the moment, I'm storing this in an AtomicReference, and using synchrnonized blocks (synchronized on the AtomicReference itself, not its stored…
Edward Peters
  • 3,623
  • 2
  • 16
  • 39
1
2 3 4