Equivalent to AtomicReference, but without the cost of volatile synchronization

Which is equivalent:

AtomicReference<SomeClass> ref = new AtomicReference<SomeClass>( ... ); 

but without the cost of synchronization. Note that I want to wrap the link inside another object.

I looked at classes extending the abstract abstract class, but I lost a little among all the options.

I need something really simple, not weak and phantom, as well as all other links except one. Which class should I use?

+5
source share
9 answers

If you are just trying to save a link in an object. Can you create a class with a field, given that the field will be a strong reference that should achieve what you want.

You should not create a StrongReference class (because that would be stupid), but demonstrate it

 public class StrongReference{ Object refernece; public void set(Object ref){ this.reference =ref; } public Object get(){ return this.reference; } } 
+3
source

If you need a link without thread safety, you can use an array of them.

 MyObject[] ref = { new MyObject() }; MyObject mo = ref[0]; ref[0] = n; 
+5
source

AtomicReference does not have the cost of synchronization in the sense of traditional synchronized partitions. It is implemented as non-blocking, which means that threads that are waiting for “getting a lock” do not go into context, which makes it very fast in practice. You probably cannot find a faster method to update one link at a time.

+2
source

If you still want to use AtomicReference but don’t want to cover the costs of flying, you can use lazySet

Recording does not create a memory barrier that makes normal volatile recording, but still causes a volatile load (which is relatively cheap).

 AtomicReference<SomeClass> ref = new AtomicReference<SomeClass>(); ref.lazySet(someClass); 
+2
source

I think all you need is:

 public class MyReference<T>{ T reference; public void set(T ref){ this.reference =ref; } public T get(){ return this.reference; } } 

You might want to add delegation equals (), hashcode (), and toString ().

+2
source

The cost of AtomicReference is not related to synchronization. From the java.util.concurrent.atomic package description:

A small set of tools for classes that support uncommitted thread-safe programming for single variables.

EDIT Based on your comments on your original post, it seems that you used the term “cost of synchronization” in a non-standard way to mean that the stream is in the local stream cache in general. On most architectures, reading volatile almost as cheap as reading non-volatile values. Any update to a shared variable will require clearing the cache of at least that variable (unless you intend to completely destroy the thread-local caches). There is nothing cheaper (in performance) than the classes in java.util.concurrent.atomic.

0
source

all of the provided classes that extend the Reference have some special functionality related to atomic CaS to allow the object referenced by the object to collect the thoguh event, a link still exists for the object

you can create your own StringReference as John Vint explained (or use an array with length == 1), but there are not many uses for this, though

0
source

If your value is immutable, java.util.Optional looks like a great option.

0
source

Starting with Java 9, you can now use AtomicReference.setPlain() and AtomicReference.getPlain() .

JavaDoc on setPlain : Msgstr "Sets the value to newValue with the semantics of the installation memory, as if the variable were declared non-volatile and not final."

0
source

Source: https://habr.com/ru/post/1384144/


All Articles