문제

From there

Trying to introduce memoization into the recursion algorithm.

case class Memo[A,B](f: A => B) extends (A => B) {
  private val cache = mutable.Map.empty[A, B]
  def apply(x: A) = cache getOrElseUpdate (x, f(x))
}

private val fib: Memo[Int, BigInt] = Memo {
  case 0 => 0
  case 1 => 1
  case n => fib(n-1) + fib(n-2) 
}

def foo(n: Int) = {
  fib(n)
} 

Does it mean that if we use mutable state and functional value (defined by val.. well almost functional value) then it is not thread-safe?

(val fib - looks like global scoped mutable variable/object)

도움이 되었습니까?

해결책

I'd say yes, there can be a problem if more threads are calling foo at the same time. Even though I was thinking that if you're only adding to the map and always the same value for a key, there is no guarantee that it must work. If implementation of getOrElseUpdate method of Map is for whatever reason stateful, it would not work. The contract of mutable Map is by definition not thread-safe.

If you can, add synchronization to your map and you're fine:

private val cache = new HashMap[A, B] with SynchronizedMap[A, B]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top