Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

FWIW, there has been a thread-safe implementation of putIfAbsent(K, V) since ConcurrentMap was introduced in 1.5. computeIfAbsent just adds the additional property of avoiding possibly computing the value more than once if there is a race in

    if (!map.containsKey(key)) {
        V value = compute(key);
        V existing = map.putIfAbsent(key, value);
        if (existing != null) {
            // someone else won the race
        }
    }


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: