minor reformatting for AtomicsUtil
This commit is contained in:
@@ -6,18 +6,29 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.atomic.AtomicReferenceArray;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class AtomicsUtil {
|
||||
// While java 8 does have built in atomic operations, there doesn't seem to be any Compare And Exchange operation...
|
||||
// So here we implement our own.
|
||||
|
||||
public static <T> T compareAndExchange(AtomicReference<T> atomic, T expected, T newValue) {
|
||||
while (true) {
|
||||
T oldValue = atomic.get();
|
||||
if (oldValue != expected) return oldValue;
|
||||
if (atomic.weakCompareAndSet(expected, newValue)) return expected;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* While java 8 does have built in atomic operations, there doesn't seem to be any Compare And Exchange operation... <br>
|
||||
* So here we implement our own.
|
||||
*/
|
||||
public class AtomicsUtil
|
||||
{
|
||||
|
||||
public static <T> T compareAndExchange(AtomicReference<T> atomic, T expected, T newValue)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
T oldValue = atomic.get();
|
||||
if (oldValue != expected)
|
||||
{
|
||||
return oldValue;
|
||||
}
|
||||
else if (atomic.weakCompareAndSet(expected, newValue))
|
||||
{
|
||||
return expected;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> BooleanObjectImmutablePair<T> compareAndExchangeWeak(AtomicReference<T> atomic, T expected, T newValue) {
|
||||
T oldValue = atomic.get();
|
||||
if (oldValue == expected && atomic.weakCompareAndSet(expected, newValue)) {
|
||||
@@ -26,7 +37,7 @@ public class AtomicsUtil {
|
||||
return new BooleanObjectImmutablePair<>(false, oldValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static <T> T conditionalAndExchange(AtomicReference<T> atomic, Predicate<T> requirement, T newValue) {
|
||||
while (true) {
|
||||
T oldValue = atomic.get();
|
||||
@@ -34,7 +45,7 @@ public class AtomicsUtil {
|
||||
if (atomic.weakCompareAndSet(oldValue, newValue)) return oldValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static <T> BooleanObjectImmutablePair<T> conditionalAndExchangeWeak(AtomicReference<T> atomic, Predicate<T> requirement, T newValue) {
|
||||
T oldValue = atomic.get();
|
||||
if (requirement.test(oldValue) && atomic.weakCompareAndSet(oldValue, newValue)) {
|
||||
@@ -43,9 +54,11 @@ public class AtomicsUtil {
|
||||
return new BooleanObjectImmutablePair<>(false, oldValue);
|
||||
}
|
||||
}
|
||||
|
||||
// Additionally, we implement some helper methods for frequently used atomic operations.
|
||||
|
||||
|
||||
|
||||
|
||||
// Additionally, we implement some helper methods for frequently used atomic operations. //
|
||||
|
||||
// Compare with expected value and set new value if equal. Then return whatever value the atomic now contains.
|
||||
public static <T> T compareAndSetThenGet(AtomicReference<T> atomic, T expected, T newValue) {
|
||||
while (true) {
|
||||
@@ -54,9 +67,11 @@ public class AtomicsUtil {
|
||||
if (atomic.weakCompareAndSet(expected, newValue)) return newValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Below is the array version of the above.
|
||||
|
||||
|
||||
|
||||
// Below is the array version of the above. //
|
||||
|
||||
public static <T> T compareAndExchange(AtomicReferenceArray<T> array, int index, T expected, T newValue) {
|
||||
while (true) {
|
||||
T oldValue = array.get(index);
|
||||
@@ -64,7 +79,7 @@ public class AtomicsUtil {
|
||||
if (array.weakCompareAndSet(index, expected, newValue)) return expected;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static <T> BooleanObjectImmutablePair<T> compareAndExchangeWeak(AtomicReferenceArray<T> array, int index, T expected, T newValue) {
|
||||
T oldValue = array.get(index);
|
||||
if (oldValue == expected && array.weakCompareAndSet(index, expected, newValue)) {
|
||||
@@ -73,7 +88,7 @@ public class AtomicsUtil {
|
||||
return new BooleanObjectImmutablePair<>(false, oldValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static <T> T compareAndSetThenGet(AtomicReferenceArray<T> array, int index, T expected, T newValue) {
|
||||
while (true) {
|
||||
T oldValue = array.get(index);
|
||||
@@ -81,5 +96,5 @@ public class AtomicsUtil {
|
||||
if (array.weakCompareAndSet(index, expected, newValue)) return newValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user