Made methods return boolean to indicate whether the update was

successful or not.
This commit is contained in:
Simone Bordet 2014-09-12 12:58:35 +02:00
parent 0dca1b0794
commit ffa0fc73a9
1 changed files with 12 additions and 8 deletions

View File

@ -27,47 +27,51 @@ public class Atomics
{ {
} }
public static void updateMin(AtomicLong currentMin, long newValue) public static boolean updateMin(AtomicLong currentMin, long newValue)
{ {
long oldValue = currentMin.get(); long oldValue = currentMin.get();
while (newValue < oldValue) while (newValue < oldValue)
{ {
if (currentMin.compareAndSet(oldValue, newValue)) if (currentMin.compareAndSet(oldValue, newValue))
break; return true;
oldValue = currentMin.get(); oldValue = currentMin.get();
} }
return false;
} }
public static void updateMax(AtomicLong currentMax, long newValue) public static boolean updateMax(AtomicLong currentMax, long newValue)
{ {
long oldValue = currentMax.get(); long oldValue = currentMax.get();
while (newValue > oldValue) while (newValue > oldValue)
{ {
if (currentMax.compareAndSet(oldValue, newValue)) if (currentMax.compareAndSet(oldValue, newValue))
break; return true;
oldValue = currentMax.get(); oldValue = currentMax.get();
} }
return false;
} }
public static void updateMin(AtomicInteger currentMin, int newValue) public static boolean updateMin(AtomicInteger currentMin, int newValue)
{ {
int oldValue = currentMin.get(); int oldValue = currentMin.get();
while (newValue < oldValue) while (newValue < oldValue)
{ {
if (currentMin.compareAndSet(oldValue, newValue)) if (currentMin.compareAndSet(oldValue, newValue))
break; return true;
oldValue = currentMin.get(); oldValue = currentMin.get();
} }
return false;
} }
public static void updateMax(AtomicInteger currentMax, int newValue) public static boolean updateMax(AtomicInteger currentMax, int newValue)
{ {
int oldValue = currentMax.get(); int oldValue = currentMax.get();
while (newValue > oldValue) while (newValue > oldValue)
{ {
if (currentMax.compareAndSet(oldValue, newValue)) if (currentMax.compareAndSet(oldValue, newValue))
break; return true;
oldValue = currentMax.get(); oldValue = currentMax.get();
} }
return false;
} }
} }