From what I understand, we can initialize an empty mutable HashMap as
var keyCountsMap :scala.collection.mutable.Map[Any, Int] = scala.collection.mutable.Map[Any, Int]()
But how do I initialize this HasMap as synchronized ? I tried
var keyCountsMap :scala.collection.mutable.SynchronizedMap[Any, Int] = scala.collection.mutable.Map[Any, Int]()
but it I get the following error:
type mismatch; found : scala.collection.mutable.Map[Any,Int]
required: scala.collection.mutable.SynchronizedMap[Any,Int]
答案 0 :(得分:3)
您可以混合SynchronizedMap(deprecated in 2.11)
var keyCountsMap = new scala.collection.mutable.HashMap[Any, Int]() with scala.collection.mutable.SynchronizedMap[Int, Int]
答案 1 :(得分:3)
如果您查看文档,您会看到:
此类应用作 mixin 。它同步Map 它所混入的类的函数。
和
通过traits同步已被弃用,因为它本身就是这样 不可靠的。将 java.util.concurrent.ConcurrentHashMap 视为一个 替代品。