11package com .fasterxml .jackson .databind .deser ;
22
33import java .util .HashMap ;
4+ import java .util .concurrent .locks .ReentrantLock ;
45
56import com .fasterxml .jackson .annotation .JsonFormat ;
67import com .fasterxml .jackson .databind .*;
@@ -52,6 +53,12 @@ public final class DeserializerCache
5253 protected final HashMap <JavaType , JsonDeserializer <Object >> _incompleteDeserializers
5354 = new HashMap <JavaType , JsonDeserializer <Object >>(8 );
5455
56+
57+ /**
58+ * We hold an explicit lock while creating deserializers to avoid creating duplicates.
59+ */
60+ private final ReentrantLock _incompleteDeserializersLock = new ReentrantLock ();
61+
5562 /*
5663 /**********************************************************
5764 /* Life-cycle
@@ -242,10 +249,12 @@ protected JsonDeserializer<Object> _createAndCacheValueDeserializer(Deserializat
242249 DeserializerFactory factory , JavaType type )
243250 throws JsonMappingException
244251 {
245- // Only one thread to construct deserializers at any given point in time;
246- // limitations necessary to ensure that only completely initialized ones
247- // are visible and used.
248- synchronized (_incompleteDeserializers ) {
252+ /* Only one thread to construct deserializers at any given point in time;
253+ * limitations necessary to ensure that only completely initialized ones
254+ * are visible and used.
255+ */
256+ _incompleteDeserializersLock .lock ();
257+ try {
249258 // Ok, then: could it be that due to a race condition, deserializer can now be found?
250259 JsonDeserializer <Object > deser = _findCachedDeserializer (type );
251260 if (deser != null ) {
@@ -268,6 +277,8 @@ protected JsonDeserializer<Object> _createAndCacheValueDeserializer(Deserializat
268277 _incompleteDeserializers .clear ();
269278 }
270279 }
280+ } finally {
281+ _incompleteDeserializersLock .unlock ();
271282 }
272283 }
273284
0 commit comments