From 6884072392e9f891c4a5b5edc8d59110607b70ad Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 27 Mar 2024 20:02:03 +0100 Subject: [PATCH] use ReentrantLock in ThreadLocalBufferManager --- .../jackson/core/util/ThreadLocalBufferManager.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/core/util/ThreadLocalBufferManager.java b/src/main/java/com/fasterxml/jackson/core/util/ThreadLocalBufferManager.java index 6dbe7a594a..2a471cc4f7 100644 --- a/src/main/java/com/fasterxml/jackson/core/util/ThreadLocalBufferManager.java +++ b/src/main/java/com/fasterxml/jackson/core/util/ThreadLocalBufferManager.java @@ -5,6 +5,7 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantLock; /** * For issue [jackson-core#400] We keep a separate Set of all SoftReferences to BufferRecyclers @@ -23,7 +24,7 @@ class ThreadLocalBufferManager * A lock to make sure releaseBuffers is only executed by one thread at a time * since it iterates over and modifies the allSoftBufRecyclers. */ - private final Object RELEASE_LOCK = new Object(); + private final ReentrantLock RELEASE_LOCK = new ReentrantLock(); /** * A set of all SoftReferences to all BufferRecyclers to be able to release them on shutdown. @@ -64,8 +65,9 @@ public static ThreadLocalBufferManager instance() { * It will clear all bufRecyclers from the SoftRefs and release all SoftRefs itself from our set. */ public int releaseBuffers() { - synchronized (RELEASE_LOCK) { - int count = 0; + int count = 0; + RELEASE_LOCK.lock(); + try { // does this need to be in sync block too? Looping over Map definitely has to but... removeSoftRefsClearedByGc(); // make sure the refQueue is empty for (SoftReference ref : _trackedRecyclers.keySet()) { @@ -73,8 +75,10 @@ public int releaseBuffers() { ++count; } _trackedRecyclers.clear(); //release cleared SoftRefs - return count; + } finally { + RELEASE_LOCK.unlock(); } + return count; } public SoftReference wrapAndTrack(BufferRecycler br) {