Thread-safe?

15,796 views
Skip to first unread message

Jacob

unread,
Oct 9, 2008, 2:43:07 PM10/9/08
to google-gson
Is a Gson object thread safe and safe to inject via a framework like
spring?

Inderjeet Singh

unread,
Oct 9, 2008, 9:15:18 PM10/9/08
to googl...@googlegroups.com
It should be. If it is not, then it would be a bug that we will commit to fix.

Inder

bs

unread,
Oct 14, 2008, 2:05:03 PM10/14/08
to google-gson
I don't think it is thread safe. Here is an exception that I'm
getting when using a single Gson object from multiple threads:

java.util.ConcurrentModificationException
at java.util.AbstractList
$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at com.google.gson.MemoryRefStack.contains(MemoryRefStack.java:75)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:127)
at
com.google.gson.JsonSerializationVisitor.getJsonElementForChild(JsonSerializationVisitor.java:
150)
at
com.google.gson.JsonSerializationVisitor.addAsChildOfObject(JsonSerializationVisitor.java:
124)
at
com.google.gson.JsonSerializationVisitor.visitPrimitiveField(JsonSerializationVisitor.java:
159)
at
com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:
177)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:151)
at
com.google.gson.JsonSerializationContextDefault.serialize(JsonSerializationContextDefault.java:
47)
at com.google.gson.Gson.toJson(Gson.java:272)
at com.google.gson.Gson.toJson(Gson.java:228)
at com.google.gson.Gson.toJson(Gson.java:208)
...

On Oct 9, 9:15 pm, "Inderjeet Singh" <inder...@gmail.com> wrote:
> It should be. If it is not, then it would be a bug that we will commit to fix.
>
> Inder
>

inde...@gmail.com

unread,
Oct 14, 2008, 3:11:03 PM10/14/08
to google-gson
Is it possible for you to post a test that we can use to reproduce
this problem?

Thanks
Inder

Bill Simons

unread,
Oct 14, 2008, 3:14:38 PM10/14/08
to googl...@googlegroups.com
Sure. Can you run testNG tests or do you need JUnit?

Inderjeet Singh

unread,
Oct 14, 2008, 8:26:57 PM10/14/08
to googl...@googlegroups.com
JUnit will be best, but as long as you provide us a code snippet, we
can integrate it ourselves.

Thanks
Inder

Bill Simons

unread,
Oct 15, 2008, 10:41:47 AM10/15/08
to googl...@googlegroups.com
Here you go:

import com.google.gson.Gson;
import static org.junit.Assert.assertFalse;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

public class GsonTest {

@Test
public void testSingleThread() {
Gson gson = new Gson();
MyObj myObj = new MyObj("hello", "world", 42);
for (int i = 0; i < 10; i++) {
gson.toJson(myObj);
}
}

@Test
public void testMultiThread() throws InterruptedException {
final Gson gson = new Gson();

final CountDownLatch startLatch = new CountDownLatch(1);
final CountDownLatch finishedLatch = new CountDownLatch(10);
final AtomicBoolean failed = new AtomicBoolean(false);

ExecutorService executor = Executors.newFixedThreadPool(10);

for (int taskCount = 0; taskCount < 10; taskCount++) {
executor.execute(new Runnable() {
public void run() {
MyObj myObj = new MyObj("hello", "world", 42);
try {
startLatch.await();
for (int i = 0; i < 10; i++) {
gson.toJson(myObj);
}
} catch (Throwable t) {
failed.set(true);
} finally {
finishedLatch.countDown();
}
}
});
}

startLatch.countDown();
finishedLatch.await();
assertFalse(failed.get());
}

private class BooleanWrapper {
public boolean failed;
}

private class MyObj {
private String a;
private String b;
private int i;

public MyObj() {
}


public MyObj(String a, String b, int i) {
this.a = a;
this.b = b;
this.i = i;
}

public String getA() {
return a;
}

public void setA(String a) {
this.a = a;
}

public String getB() {
return b;
}

public void setB(String b) {
this.b = b;
}

public int getI() {
return i;
}

public void setI(int i) {
this.i = i;
}
}
}

inde...@gmail.com

unread,
Oct 16, 2008, 12:50:58 AM10/16/08
to google-gson
Thank you. Will will incorporate it in our test harness and file and
fix the bug for our next release.

Thanks
Inder

Bill Simons

unread,
Oct 16, 2008, 9:20:24 AM10/16/08
to googl...@googlegroups.com
Excellent. Can you post the issue number so I can "watch" it?

inde...@gmail.com

unread,
Oct 20, 2008, 2:31:08 PM10/20/08
to google-gson

inde...@gmail.com

unread,
Oct 20, 2008, 4:16:18 PM10/20/08
to google-gson
This bug has been fixed and the bugfix will be included in the next
release.
Thanks to Bill for providing an excellent test.

sdcma...@gmail.com

unread,
Apr 4, 2017, 2:00:38 PM4/4/17
to google-gson
Hi Inderjeet, 

I was facing the same issue, so I upgraded to version 2.8.0. 

I still am encountering the issue:

java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1437)
at java.util.HashMap$KeyIterator.next(HashMap.java:1461)
at java.util.Collections$UnmodifiableCollection$1.next(Collections.java:1042)
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.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)
at com.google.gson.Gson.toJson(Gson.java:583)

any thoughts ?  

Inderjeet Singh

unread,
Apr 4, 2017, 2:52:00 PM4/4/17
to google-gson
Is it possible for you to create a test that reproduces the problem?
Reply all
Reply to author
Forward
0 new messages