The ConcurrentMap section in the docs claim:
m putIfAbsent(k, v) Adds key/value binding k -> v unless k is already defined in m
but in reality, I found putIfAbsent
always tries to insert v
. The way to get around this is to use getOrElseUpdate
, but is this really a bug, or am I missing something?
My code is the following:
val instanceMap: ConcurrentMap[Address, (MongodExecutable, MongodProcess)] =
(new JavaConcurrentMap[Address, (MongodExecutable, MongodProcess)]()).asScala
def start(host: String = DefaultAddress.host, port: Int = DefaultAddress.port): MongodProcess = {
val addr = Address(host, port)
instanceMap.putIfAbsent(addr, { // this block is ALWAYS executed
val mongodExecutable = starter.prepare(mongodConfig(host, port))
val mongod = mongodExecutable.start()
logProcessInfo(mongod)
(mongodExecutable, mongod)
})
instanceMap(addr)
._2
}
putIfAbsent
takes a value, not a lambda.