In my Scala application I need to use several Maps and Lists which gets updated very often. Are there any thread safe collections in Scala which maintain the insertion order?
1 Answer
Yes, there is a trait in the scala.collection.concurrent
package: concurrent.Map it's just a trait, so just mixin this trait into your Map and it would become thread-safe.
If you need a good concurrent map, try google's ConcurrentLinkedHashMap (last release 2015) and convert it to Scala Map using Scala/Java converter, that will give more performance that mixin SynchronizedMap
. For example my favourite Spray toolkit, use it as a core structure for implmenting it's caching module. As you can see, spray is the fastest Scala web toolkit
-
scala.collection.mutable.LinkedHashMap[String,Any] -> this is the map that am using. How do I make it thread safe acc to you?– yAsHCommented Jul 9, 2013 at 5:58
-
I similarly tried
new scala.collection.mutable.Map[String, Long] with scala.collection.concurrent.Map[String, Long]
which fails to compile:object creation impossible, since: [error] it has 8 unimplemented members.
(error message truncated to fit comment). Switching toLinkedHashMap
yields only 4 unimplemented members... I think it relates to the use ofLong
yet not sure what to make out of this... feels slightly awkward adding support for Long on my own or degressing fromLong
toAny
for this. Any thoughts?– matanoxCommented Nov 25, 2014 at 13:38 -