Hi,
according to https://groups.google.com/forum/#!topic/google-gson/Vju1HuJJUIE gson is thread safe however I am facing the following exception from time to time with gson 2.8.0
Exception in thread "Thread-17" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.Gson.toJson(Gson.java:669)
at com.google.gson.Gson.toJson(Gson.java:648)
at com.google.gson.Gson.toJson(Gson.java:603)
The Gson object is threadsafe, yes, but serialization of user objects is not. You should either make a defensive copy or use a lock when an object needs serialized but can also be manipulated from other threads concurrently.
Most helpful comment
The Gson object is threadsafe, yes, but serialization of user objects is not. You should either make a defensive copy or use a lock when an object needs serialized but can also be manipulated from other threads concurrently.