4

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 1

5

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

3
  • scala.collection.mutable.LinkedHashMap[String,Any] -> this is the map that am using. How do I make it thread safe acc to you?
    – yAsH
    Commented 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 to LinkedHashMap yields only 4 unimplemented members... I think it relates to the use of Long yet not sure what to make out of this... feels slightly awkward adding support for Long on my own or degressing from Long to Any for this. Any thoughts?
    – matanox
    Commented Nov 25, 2014 at 13:38
  • What about lists then?
    – Machisuji
    Commented Jun 5, 2015 at 15:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.