Equivalent of AtomicReference but without the volatile synchronization cost

AtomicReference does not have the cost of synchronization in the sense of traditional synchronized sections. It is implemented as non-blocking, meaning that threads that wait to "acquire the lock" are not context-switched, which makes it very fast in practice. Probably for concurrently updating a single reference, you cannot find a faster method.


If you are simply trying to store a reference in an object. Can't you create a class with a field, considering the field would be a strong reference that should achieve what you want

You shouldn't create a StrongReference class (because it would be silly) but to demonstrate it

public class StrongReference{
  Object refernece;

  public void set(Object ref){
    this.reference =ref;
  }
  public Object get(){
    return this.reference;
  }

}

Since Java 9 you can now use AtomicReference.setPlain() and AtomicReference.getPlain().

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


If you want a reference without thread safety you can use an array of one.

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

Tags:

Java

Reference