Code example for AtomicReference

Methods: compareAndSet, get

0
    public CheckThenActBase(T data){
        this.dataPoint = new AtomicReference<T>(data);
    } 
 
    public void OptimisticUpdate(Updater<T> toExecute) {
        T initialValue = dataPoint.get();
        T newPoint = toExecute.update(GetSnapshot());
 
        if (setDataPoint(initialValue, newPoint)){
            return; 
        }else{ 
            collisions.getAndIncrement();
            OptimisticUpdate(toExecute);
        } 
    } 
 
    public T GetSnapshot(){ 
        return dataPoint.get().Clone();
    } 
 
    private boolean setDataPoint(T expected, T newValue){
        return dataPoint.compareAndSet(expected, newValue);
    } 
 
}