Is there a smarter way than ?
....
synchronized (myMap) {
if (myMap.size() < 5) {
myMap.putIfAbsent("something", true);
}
}
...
UPDATE:
After trying several things I think I might have come up with something better using an AtomicInteger dictating if we can add to the map
if (count.accumulateAndGet(1, (left, right) -> left >= 6 ? 6 : left + right ) <= 5) {
myMap.putIfAbsent("something", true);
}