Skip to content

Instantly share code, notes, and snippets.

@mrogaski
Created January 19, 2014 15:18
Show Gist options
  • Save mrogaski/8506237 to your computer and use it in GitHub Desktop.
Save mrogaski/8506237 to your computer and use it in GitHub Desktop.
ConcurrentHashMap getOrElseProduce()
import akka.dispatch.Future._
def getOrElseProduce(key: K, producer: => V): Future[V] = {
val f = empty[V]()
putIfAbsent(key, f) match {
case null => f completeWithResult producer
case value => value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment