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
1
vote
3 answers

Race conditions with java references

The atomic integer, long, boolean etc are used to do any atomic updates to the respective types since there may be a race condition when we execute any manipulation on them, e.g ++. But what are the different cases with references where there may be…
keshav84
  • 2,291
  • 5
  • 25
  • 34
1
vote
2 answers

How to add hostname to block list after consecutive failures in multithreading application?

I am using Callable in my code which will be called by multiple threads as shown below. As of now, whenever any RestClientException is thrown then I am adding hostname to blockList. public class Task implements Callable { private…
john
  • 11,311
  • 40
  • 131
  • 251
1
vote
0 answers

Hazelcast - AtomicReference.alterAndGet is throwning HazelcastSerializationException

I'm running three Hazelcast nodes on the same machine, each node tries to change an AtomicReference. The first and second node are running without problems, the third node is throwning HazelcastSerializationException. Hazelcast version: Hazelcast…
Silvano Lohn
  • 41
  • 1
  • 2
1
vote
1 answer

Atomic references are unnecessary when using synchronized

This code came from the book Java Concurrency Guidelines by Fred Long. I understand that a group of atomic operations is not an atomic operation. So the code below is non-compliant. To find the code, please take a look at page 23. public class Adder…
Maksim Dmitriev
  • 5,985
  • 12
  • 73
  • 138
1
vote
2 answers

Lock-free and size-restricted queue in Java

I'm trying to extend an implementation of a lock-free queue in Java according to this posting. For my implemenation I am restricted in using only atomic variables/references. The addition is that my queue should have a maximum size. And therefore a…
Steve Murdock
  • 709
  • 1
  • 10
  • 20
1
vote
2 answers

Correct understanding of concepts like volatile, syncronized and AtomicReference?

I would like to be sure I understood correctly these concepts. The explanations/confirmations will help me a lot and I'm sure many other programmers. So here are my understandings of these concepts, resulted from my investigation: Volatile is used…
1
vote
1 answer

Why does boost::shared_ptr bother with atomic reference counting when it's not thread-safe?

This is more of a curiosity question, but as boost::shared_ptr is not thread-safe, why would it then bother using atomic reference counting? Since the destructor is not safe to use across threads, I'm failing to see the point. For example, if you…
Anthony
  • 245
  • 1
  • 2
  • 10
0
votes
2 answers

How to create a reference to the main activity via a tabhost

I need to create a reference to this particular class from a tabhost in my application. The reason im creating this reference is to access some values in this particular class. but when i create a constructor in my tabhost activity it throws an a…
user1187383
0
votes
1 answer

AtomicReference getAndSet not working as expected

I have a class called LocalIpBlockingDenyListResponse that has two class members viz a List customClassList and an int storing hashCode() of customClassList I have atomic reference of above class defined in another class like, private…
LearningAsIGo
  • 1,240
  • 2
  • 11
  • 23
0
votes
0 answers

Java threadsafe single update

I have a scenario where threads using single object and in case of a failure, update object once and other getters/threads uses the new updated object. I am planning to use AtomicReference but not sure if updateAndGet makes sure of achieving this. I…
Olgun Kaya
  • 2,519
  • 4
  • 32
  • 46
0
votes
0 answers

AtomicRef's compareAndSet in Kotlin

I am implementing an Elimination back-off stack and I'm confronting a problem trying to invoke compareAndSet on AtomicRef. I have a function called "write": private fun write(elementRef: E, index: Int): AtomicRef? { val positionRef:…
0
votes
0 answers

Android CountDownTimer won't work in Runnable concurrent task

I have an Android application which has an onClick() handler from a button that starts an asynchronous Runnable task. Partway through, it's supposed to start a new CountDownTimer of 5 seconds, update the text on the page onTick(), and onFinish()…
0
votes
1 answer

Concat new characters to StringBuilder object atomically

My problem is: I have class: public class AtomicStringBuilder { private final AtomicReference sbRef; } I need to add new characters to StringBuilder concurrently and atomically. But problem is, only last 128 characters should be…
0
votes
1 answer

Does AtomicReference.compareAndSet(old,new) guarantees old.field wasn't change?

Does AtomicReference.compareAndSet(old,new) guarantee old.field wasn't change? Or it just guarantees old wasn't reassign to a new object? if 2 is true, does it mean AtomicReference is useful only with immutable objects like String? (Because in…
J.J. Beam
  • 2,612
  • 2
  • 26
  • 55
0
votes
0 answers

Hibernate @Enumerated as AtomicReference

I have a class that uses Hiberante for database operations and need it to be thread-safe. I am trying refactor the code to use Atomic Variables with its Operations instead of having a synchronized method due to locking as it is breaking the…
Tomeister
  • 675
  • 2
  • 9
  • 26