Search code examples
javamultithreadingconcurrencyjava.util.concurrent

Guarantees of AtomicReference


What are the semantics of an AtomicReference?
If I do:

AtomicReference<CustomObject> ref = new AtomicReference<CustomObject>();

and then I do:

public void someMethod(){  
 //do something  

 ref.set(loadNewData());  

}  

private final Sempahore updatePermit = new Sempahore(1);  

private CustomObject loadNewData(){  
     CustomObject obj = null;  
     if (updatePermit.tryAcquire()) {  
        obj = ...; //do the loading and create Object  
        updatePermit.release();  
    } else {  
        //update already running, wait  
        updatePermit.acquire();  
        //release the permit immediately  
        updatePermit.release();  
        obj = ref.get(); //????
    }  
    return obj;    
}    

Is there a guarantee that on line obj = ref.get(); //???? the get will return the most fresh version of CustomObject?
This is related to the answer of assylias for post:


Solution

  • Actually, your code has a race condition which could cause new data to be lost:

    1. first caller goes into loadNewData and starts loading new data (has semaphore)
    2. second caller cannot get semaphore and waits at acquire
    3. first caller finishes loading and releases semaphore (but doesn't return new data yet)
    4. second caller acquires semaphore, calls ref.get() and gets old data
    5. first caller returns new data which is passed to ref.set()
    6. second caller return old data which overwrites new data when passed to ref.set()

    Since someMethod() is always loading new data and you always want callers to wait while new data is loading, all this extra stuff is useless. just use a simple synchronized block (or Lock) around the whole block and ditch the atomic reference. according to the linked post, it seems like you only ever want to do this once, so use an initialized flag.

    private boolean _initialized;
    
    public synchronized void loadLatestData() {
      if(!_initialized) {
          // load latest data ...
          _initialized = true;
      }
    }