Closing over java.util.concurrent.ConcurrentHashMap inside a Future ?

66 views
Skip to first unread message

Soumya Simanta

unread,
Apr 25, 2015, 6:28:56 AM4/25/15
to akka...@googlegroups.com
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. 

1. Don't use futures inside the Actor's receive method. In this may have a negative impact on both latency as well as throughput in case I've a large number of gets/puts. because both will be performed in sequence. 
2. Use java.util.concurrent.ConcurrentHashMap and then invoke the gets and puts inside a Future. 


Given that java.util.concurrent.ConcurrentHashMap is thread-safe, I was wondering if it is still a problem to close over the concurrentHashMap inside a Future created for each put and get

PS: 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? 




Jeroen Gordijn

unread,
Apr 25, 2015, 9:45:04 AM4/25/15
to akka...@googlegroups.com
Hi Soumya,

Do agents cover your usecase?
http://doc.akka.io/docs/akka/snapshot/scala/agents.html

Cheers,
Jeroen

Soumya Simanta

unread,
Apr 25, 2015, 8:44:08 PM4/25/15
to akka...@googlegroups.com
@Jeroen - Thanks. I've not looked at agents yet. 

Konrad Malawski

unread,
Apr 27, 2015, 6:38:55 AM4/27/15
to Akka User List
Hi Soumya,
Why do you have the concurrent hash map in the Actor at all?
When using Actors you can easily have a var with an immutable.Map
which you can update/read from the Actor's receive. If you need to use Futures,
apply the pipeTo pattern to forward the action to self.

Of course, if you actually have benchmarked this and observe that this is not fast enough
(though it should be fast enough for many use-cases) you could drop down to mutable things.

The reason I'd advertise to use vars with immutable datastructures is that you don't
have the problem of accidentally sending a mutable thing over to someone - in case
of the immutable Map you'd send an outdated version, but at least noone can break it.

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,
Konrad 'ktoso' Malawski
Reply all
Reply to author
Forward
0 new messages