I suppose you could do something like this pre-Java 8:
int val = atomicInt.get(); boolean success = false; while(val > 0 && !success) { success = atomicInt.compareAndSet(val, val - 1); if(!success) { // Try again if the value is still > 0 val = atomicInt.get(); } } // Check 'success' to see if it worked
Not the most elegant code, but I think it does the trick.
BarrySW19
source share