From 88efcf2f11b136ef38da66c27d06b8d6da951055 Mon Sep 17 00:00:00 2001 From: Mikko Tiihonen Date: Mon, 1 Apr 2013 22:31:45 +0300 Subject: [PATCH] Make keyPool synchronized to avoid stuck threads when multiple threads operate on their own JSONObjects instances concurrently --- JSONObject.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index 399f093c1..d442aff31 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -1128,14 +1128,16 @@ public JSONObject put(String key, Object value) throws JSONException { } if (value != null) { testValidity(value); - pooled = (String)keyPool.get(key); - if (pooled == null) { - if (keyPool.size() >= keyPoolSize) { + synchronized (JSONObject.class) { + pooled = (String)keyPool.get(key); + if (pooled == null) { + if (keyPool.size() >= keyPoolSize) { keyPool = new HashMap(keyPoolSize); - } - keyPool.put(key, key); - } else { - key = pooled; + } + keyPool.put(key, key); + } else { + key = pooled; + } } this.map.put(key, value); } else {