I would like to update the value vold
of a TrieMap
t
for some key k
iff the new value vnew
some predicate p(vold, vnew)
is true
. Since values can be modified through multiple threads, a synchronized
block is needed. The code could t.synchronized
but that would block updates to other keys' values. Is it possible to block only for the key that's actually being updated?
Now that I've written out the question, does it make sense to t.keySet.filter(_ == k).head.synchronized
(since there's no guarantee that k
is the same instance of the key in t
)? Is there a more canonical way to do this?