Skip to content

Commit dcc1f0b

Browse files
committed
remove synchronized block
1 parent 6224569 commit dcc1f0b

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/DeserializerCache.java

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.databind.deser;
22

33
import java.util.HashMap;
4+
import java.util.concurrent.locks.ReentrantLock;
45

56
import com.fasterxml.jackson.annotation.JsonFormat;
67
import com.fasterxml.jackson.databind.*;
@@ -52,6 +53,11 @@ public final class DeserializerCache
5253
protected final HashMap<JavaType, JsonDeserializer<Object>> _incompleteDeserializers
5354
= new HashMap<JavaType, JsonDeserializer<Object>>(8);
5455

56+
/**
57+
* We hold an explicit lock while creating deserializers to avoid creating duplicates.
58+
*/
59+
private final ReentrantLock _incompleteDeserializersLock = new ReentrantLock();
60+
5561
/*
5662
/**********************************************************
5763
/* Life-cycle
@@ -246,12 +252,26 @@ protected JsonDeserializer<Object> _createAndCacheValueDeserializer(Deserializat
246252
* limitations necessary to ensure that only completely initialized ones
247253
* are visible and used.
248254
*/
249-
synchronized (_incompleteDeserializers) {
250-
// Ok, then: could it be that due to a race condition, deserializer can now be found?
251-
JsonDeserializer<Object> deser = _findCachedDeserializer(type);
252-
if (deser != null) {
253-
return deser;
255+
if (type == null) {
256+
throw new IllegalArgumentException("Null JavaType passed");
257+
}
258+
if (_hasCustomHandlers(type)) {
259+
return null;
260+
}
261+
final boolean isCustom = _hasCustomHandlers(type);
262+
JsonDeserializer<Object> deser = isCustom ? null : _cachedDeserializers.get(type);
263+
if (deser != null) {
264+
return deser;
265+
}
266+
_incompleteDeserializersLock.lock();
267+
try {
268+
if (!isCustom) {
269+
deser = _cachedDeserializers.get(type);
270+
if (deser != null) {
271+
return deser;
272+
}
254273
}
274+
// Ok, then: could it be that due to a race condition, deserializer can now be found?
255275
int count = _incompleteDeserializers.size();
256276
// Or perhaps being resolved right now?
257277
if (count > 0) {
@@ -269,6 +289,8 @@ protected JsonDeserializer<Object> _createAndCacheValueDeserializer(Deserializat
269289
_incompleteDeserializers.clear();
270290
}
271291
}
292+
} finally {
293+
_incompleteDeserializersLock.unlock();
272294
}
273295
}
274296

0 commit comments

Comments
 (0)