he utilizado AtomicLong muchas veces, pero nunca he tenido que utilizar AtomicReferenceCuándo usar AtomicReference (Java)? ¿Es realmente necesario?
Parece que AtomicReference hace bien (he copiado el código de otra cuestión stackoverflow ):
public synchronized boolean compareAndSet(List<Object> oldValue, List<Object> newValue) {
if (this.someList == oldValue) {
// someList could be changed by another thread after that compare,
// and before this set
this.someList = newValue;
return true;
}
return false;
}
O
public synchronized boolean compareAndSet(List<Object> oldValue, List<Object> newValue) {
if (this.someList == oldValue || this.someList.equals(oldValue)) {
// someList could be changed by another thread after that compare,
// and before this set
this.someList = newValue;
return true;
}
return false;
}
Supongamos que this.someList se marca como volátil.
No estoy seguro de cuál es realmente porque el javadoc y el código para esa clase no son claros si se usa .equals.
Viendo cómo los métodos anteriores no son tan difíciles de escribir ¿Alguien alguna vez ha usado AtomicReference?
[Ellos] (http://www.google.com/search?q=%22import+java.util.concurrent.atomic.atomicreference%22&) alguna vez lo han usado. – BalusC