Last active
August 16, 2018 09:02
-
-
Save pavlukhin/92701277f66f8901a7feda6283a5a299 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private final ConcurrentMap<Integer, ConcurrentMap<Integer, AtomicLong>> sizeDeltas = new ConcurrentHashMap<>(); | |
// Good old approach | |
@Override public void accumulateSizeDelta(int cacheId, int part, long delta) { | |
ConcurrentMap<Integer, AtomicLong> partDeltas = sizeDeltas.get(cacheId); | |
if (partDeltas == null) { | |
ConcurrentMap<Integer, AtomicLong> partDeltas0 = | |
sizeDeltas.putIfAbsent(cacheId, partDeltas = new ConcurrentHashMap<>()); | |
if (partDeltas0 != null) | |
partDeltas = partDeltas0; | |
} | |
AtomicLong accDelta = partDeltas.get(part); | |
if (accDelta == null) { | |
AtomicLong accDelta0 = partDeltas.putIfAbsent(part, accDelta = new AtomicLong()); | |
if (accDelta0 != null) | |
accDelta = accDelta0; | |
} | |
accDelta.addAndGet(delta); | |
} | |
// Lambda approach | |
@Override public void accumulateSizeDelta1(int cacheId, int part, long delta) { | |
ConcurrentMap<Integer, AtomicLong> partDeltas = sizeDeltas.computeIfAbsent(cacheId, k -> new ConcurrentHashMap<>()); | |
AtomicLong accDelta = partDeltas.computeIfAbsent(part, k -> new AtomicLong()); | |
accDelta.addAndGet(delta); | |
} | |
// Lambda one-liner approach | |
@Override public void accumulateSizeDelta2(int cacheId, int part, long delta) { | |
sizeDeltas | |
.computeIfAbsent(cacheId, k -> new ConcurrentHashMap<>()) | |
.computeIfAbsent(part, k -> new AtomicLong()) | |
.addAndGet(delta); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment