GithubHelp home page GithubHelp logo

Comments (4)

vinc3m1 avatar vinc3m1 commented on May 17, 2024

Good catch. Originally was using getOrPut but was concerned about thread safety. Will look into this more. Happy to accept Pull Requests if you have ideas.

from stiletto.

apatrida avatar apatrida commented on May 17, 2024

Not perfect but the only hole is a race condition of N threads racing to initialize at same time could call factory more than once, but all will return same instance. Without JDK 8 with internal computeIfAbsent, it is harder without more locking to be surer. I'll see what M13 implementation looks like as well.

public inline fun <K, V> ConcurrentMap<K, V>.concurrentGetOrPut(key: K, defaultValue: () -> V): V {
    var answer = this.get(key)
    if (answer == null) {
        answer = defaultValue()
        var temp = this.putIfAbsent(key, answer!!)
        if (temp != null) {
            answer = temp
        }
    }
    return answer!!
}

from stiletto.

apatrida avatar apatrida commented on May 17, 2024

Ah, the M13 std-lib version is mostly the same logic, just prettier:

/**
 * Concurrent getOrPut, that is safe for concurrent maps.
 *
 * Returns the value for the given [key]. If the key is not found in the map, calls the [defaultValue] function,
 * puts its result into the map under the given key and returns it.
 *
 * This method guarantees not to put the value into the map if the key is already there,
 * but the [defaultValue] function may be invoked even if the key is already in the map.
 */
public inline fun <K, V: Any> ConcurrentMap<K, V>.concurrentGetOrPut(key: K, defaultValue: () -> V): V {
    // Do not use computeIfAbsent on JVM8 as it would change locking behavior
    return this.get(key) ?:
            defaultValue().let { default -> this.putIfAbsent(key, default) ?: default }

}

from stiletto.

vinc3m1 avatar vinc3m1 commented on May 17, 2024

Ah nice, makes sense. Thanks!

from stiletto.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.