I've an actor where I want to store my mutable state inside a map.
Clients can send Get(key:String)
and Put(key:String,value:String)
messages to this actor.
I'm considering the following options.
gets/puts
because all operations will be performed in order. java.util.concurrent.ConcurrentHashMap
and then invoke the gets and puts inside a Future
. Given that java.util.concurrent.ConcurrentHashMap
is thread-safe and providers finer level of granularity, I was wondering if it is still a problem to close over the concurrentHashMap inside a Future created for each put and get.
I'm aware of the fact that it's a really bad idea to close over mutable state inside a Future inside an Actor but I'm still interested to know if in this particular case it is correct or not?
In general, java.util.concurrent.ConcurrentHashMap
is made for concurrent use. As long as you don't try to transport the closure to another machine, and you think through the implications of it being used concurrently (e.g. if you read a value, use a function to modify it, and then put it back, do you want to use the replace(key, oldValue, newValue)
method to make sure it hasn't changed while you were doing the processing?), it should be fine in Futures.