diff --git a/gson/src/main/java/com/google/gson/JsonStreamParser.java b/gson/src/main/java/com/google/gson/JsonStreamParser.java
index e7a02174..3bb27501 100644
--- a/gson/src/main/java/com/google/gson/JsonStreamParser.java
+++ b/gson/src/main/java/com/google/gson/JsonStreamParser.java
@@ -23,7 +23,23 @@ import java.util.NoSuchElementException;
/**
* A streaming parser that allows reading of multiple {@link JsonElement}s from the specified reader
- * asynchronously. This class is not thread-safe.
+ * asynchronously.
+ *
+ *
This class is thread-compatible. For some more literature on these definitions, refer to
+ * Effective Java.
+ *
+ *
To properly use this class across multiple thread, you will need to add some external
+ * synchronization to your classes/thread to get this to work properly. For example:
+ *
+ *
+ * JsonStreamParser parser = new JsonStreamParser("blah blah blah");
+ * JsonElement element;
+ * synchronized (someCommonObject) {
+ * if (parser.hasNext()) {
+ * element = parser.next();
+ * }
+ * }
+ *
*
* @author Inderjeet Singh
* @author Joel Leitch
@@ -32,6 +48,7 @@ import java.util.NoSuchElementException;
public final class JsonStreamParser implements Iterator {
private final JsonParserJavacc parser;
+ private final Object lock;
private JsonElement nextElement;
/**
@@ -47,7 +64,8 @@ public final class JsonStreamParser implements Iterator {
* @since 1.4
*/
public JsonStreamParser(Reader reader) {
- parser = new JsonParserJavacc(reader);
+ parser = new JsonParserJavacc(reader);
+ lock = new Object();
nextElement = null;
}
@@ -59,11 +77,14 @@ public final class JsonStreamParser implements Iterator {
* @since 1.4
*/
public JsonElement next() throws JsonParseException {
- if (nextElement != null) {
- JsonElement returnValue = nextElement;
- nextElement = null;
- return returnValue;
+ synchronized (lock) {
+ if (nextElement != null) {
+ JsonElement returnValue = nextElement;
+ nextElement = null;
+ return returnValue;
+ }
}
+
try {
return parser.parse();
} catch (TokenMgrError e) {
@@ -84,12 +105,14 @@ public final class JsonStreamParser implements Iterator {
}
public boolean hasNext() {
- try {
- nextElement = next();
- return true;
- } catch (NoSuchElementException e) {
- nextElement = null;
- return false;
+ synchronized (lock) {
+ try {
+ nextElement = next();
+ return true;
+ } catch (NoSuchElementException e) {
+ nextElement = null;
+ return false;
+ }
}
}