According to the documentation. This is a volatile collection that can be safely used in multi-threaded applications.
Parallel hash three or TrieMap is a concurrent thread-safe lock-free implementation of the hash array associated with trie. It is used to implement simultaneous map abstraction. It has a particularly scalable concurrent insert and remove operations and memory-efficient . It supports O (1), atomic, non-fixed snapshots, which are used to implement linearized non-fixed size, iterator and clear operations. The cost of evaluating a (lazy) snapshot distributed between subsequent updates, which makes the assessment of snapshots horizontally scalable.
For more details see http://lampwww.epfl.ch/~prokopec/ctries-snapshot.pdf
It also has a really good caching API. So, for example, you need to calculate factorials of different numbers and sometimes reuse these results.
object o { val factorialsCache = new TrieMap[Int, Int]() def factorial(num: Int) = ??? // really heavy operations def doWorkWithFuctorial(num: Int) = { val factRes = factorialsCache.getOrElseUpdate(num, { // we do not want to invoke it very often factorial(num) // this function will be executed only if there are no records in Map for such key }) // start do some work `withfactRes` factRes } }
Please note that the function above uses the global state (cache) for write operations, but it is absolutely safe to use it in parallel threads. You will not lose any data.
source share