4

We have a class Document which uses a private member of type Map<String, Object> as storage. Objects of this type are kept in memory and are (very frequently) modified by (potentially) multiple threads. It also happens that these objects (or, specifically the underlying Maps) are serialized out over HTTP on request. The serialization format is Json, and the library in use is currently Google's Gson

When serialization occurs simultaneously as a modification that introduces a new Map.Entry we see ConcurrentModificationExceptions. This makes a lot of sense intuitively, since Gson is probably iterating over the entrySet which is being modified.

How can we avoid this? Do we need to resort to always passing a deep copy of the Map to Gson? In that case, how would you implement said deep copy, given that the map can contain all Json primitives, including List and Map?

1
  • 1
    how are you controlling the thread-safety of this Map in the first place? Commented Nov 30, 2012 at 3:22

1 Answer 1

4

While I question the wisdom of serializing an object that is being updated so often, using a ConcurrentHashMap should at least alleviate your concurrency issues.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.