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
@@ -246,7 +253,9 @@ protected JsonDeserializer<Object> _createAndCacheValueDeserializer(Deserializat
246253 * limitations necessary to ensure that only completely initialized ones
247254 * are visible and used.
248255 */
249- synchronized (_incompleteDeserializers ) {
256+ try {
257+ _incompleteDeserializersLock .lock ();
258+
250259 // Ok, then: could it be that due to a race condition, deserializer can now be found?
251260 JsonDeserializer <Object > deser = _findCachedDeserializer (type );
252261 if (deser != null ) {
@@ -269,6 +278,8 @@ protected JsonDeserializer<Object> _createAndCacheValueDeserializer(Deserializat
269278 _incompleteDeserializers .clear ();
270279 }
271280 }
281+ } finally {
282+ _incompleteDeserializersLock .unlock ();
272283 }
273284 }
274285
0 commit comments