Gson: ConcurrentModificationException in CollectionTypeAdapterFactory$Adapter

Created on 19 Sep 2017  路  1Comment  路  Source: google/gson

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)

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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cayhorstmann picture cayhorstmann  路  13Comments

GoogleCodeExporter picture GoogleCodeExporter  路  15Comments

inder123 picture inder123  路  17Comments

GoogleCodeExporter picture GoogleCodeExporter  路  20Comments

GoogleCodeExporter picture GoogleCodeExporter  路  14Comments