Ben AtomicLong defalarca kullandık ama AtomicReference kullanmak için gerekli aslaAtomicReference (Java) ne zaman kullanılmalıdır? Gerçekten gerekli mi?
O AtomicReference ya (ben başka stackoverflow sorudan bu kodu kopyalanan) yaptığı görünüyor:
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;
}
Ya
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;
}
Bu.someList'in uçucu olduğu varsayılır.
Hangisinin olduğundan emin değilim çünkü javadoc ve bu sınıfın kodu, .equals kullanıldığında açık değildir.
Yukarıdaki yöntemlerin tam olarak ne kadar zor yazıldığını görmek AtomicReference'ı hiç kimse kullanmadı mı?
[Onlar] (http://www.google.com/search?q=%22import+java.util.concurrent.atomic.atomicreference%22&) hiç kullanmadı. – BalusC