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?

Micha
  • 5,117
  • 8
  • 34
  • 47
yAsH
  • 3,367
  • 8
  • 36
  • 67

1 Answers1

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 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

4lex1v
  • 21,367
  • 6
  • 52
  • 86
  • scala.collection.mutable.LinkedHashMap[String,Any] -> this is the map that am using. How do I make it thread safe acc to you? – yAsH Jul 09 '13 at 05: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? – matanster Nov 25 '14 at 13:38
  • What about lists then? – Machisuji Jun 05 '15 at 15:47