Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

8
  • This is not thread safe at all. While you guarantee a single writer at a time, you can still have threads reading while the map is being modified Commented Aug 10, 2013 at 8:53
  • As I understand, all operations - read, write, mixed - that use ec as context will be thread safe. Ops outside that context will not be thread-safe. Will be glad to hear from others if this understanding is correct. Commented Aug 10, 2013 at 11:59
  • But the thing is, the reading is done directly: when you access c.counts you are not using the ExecutionContext at all. Commented Aug 10, 2013 at 12:43
  • c.counts is a mutable map so exposing it to the outside world is not a good idea. I updated the code by adding a get method and making map private. The gist is: all read/write to the mutable map must be guarded by ec. If that is not acceptable, use a concurrent data structure. Commented Aug 10, 2013 at 13:03
  • Yes, that was my whole point. This is much better now. Actually if OP can indeed adapt his code to use futures, this is the best solution in terms of ease of use and correctness (though not iintterms of speed). Commented Aug 10, 2013 at 14:15