Skip to main content
1 of 4
axel22
  • 32.2k
  • 9
  • 126
  • 139

The scala.collection.concurrent.Map trait is not meant to be mixed-in with an existing mutable Scala Map to obtain a thread-safe version of the map instance. The SynchronizedMap mixin exists for this purpose.

Currently, Scala has the scala.collection.concurrent.TrieMap implementation for the scala.collection.concurrent.Map interface, but can wrap Java classes as well.

The scala.collection.concurrent.Map, in versions prior to 2.10 known as scala.collection.mutable.ConcurrentMap, interface is used when you:

  • want to implement your own concurrent, thread-safe Map from scratch

  • want to wrap an existing Java concurrent map implementation:

E.g:

import scala.collection._
import scala.collection.convert.decorateAsScala._
import java.util.concurrent.ConcurrentHashMap

val map: concurrent.Map[String, String] = new ConcurrentHashMap().asScala
  • want to write generic code that works concurrent maps, and don't want to commit to a specific implementation:

E.g.:

import scala.collection._

def foo(map: ConcurrentMap[String, String]) = map.putIfAbsent("", "")

foo(new concurrent.TrieMap)
foo(new java.util.concurrent.ConcurrentSkipListMap)
axel22
  • 32.2k
  • 9
  • 126
  • 139