Skip to content

Commit 73def07

Browse files
authored
use ReentrantLock in ThreadLocalBufferManager (#1252)
1 parent bf6d3a0 commit 73def07

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/main/java/com/fasterxml/jackson/core/util/ThreadLocalBufferManager.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.util.*;
77
import java.util.concurrent.ConcurrentHashMap;
8+
import java.util.concurrent.locks.ReentrantLock;
89

910
/**
1011
* For issue [jackson-core#400] We keep a separate Set of all SoftReferences to BufferRecyclers
@@ -23,7 +24,7 @@ class ThreadLocalBufferManager
2324
* A lock to make sure releaseBuffers is only executed by one thread at a time
2425
* since it iterates over and modifies the allSoftBufRecyclers.
2526
*/
26-
private final Object RELEASE_LOCK = new Object();
27+
private final ReentrantLock RELEASE_LOCK = new ReentrantLock();
2728

2829
/**
2930
* A set of all SoftReferences to all BufferRecyclers to be able to release them on shutdown.
@@ -64,17 +65,20 @@ public static ThreadLocalBufferManager instance() {
6465
* It will clear all bufRecyclers from the SoftRefs and release all SoftRefs itself from our set.
6566
*/
6667
public int releaseBuffers() {
67-
synchronized (RELEASE_LOCK) {
68-
int count = 0;
68+
int count = 0;
69+
RELEASE_LOCK.lock();
70+
try {
6971
// does this need to be in sync block too? Looping over Map definitely has to but...
7072
removeSoftRefsClearedByGc(); // make sure the refQueue is empty
7173
for (SoftReference<BufferRecycler> ref : _trackedRecyclers.keySet()) {
7274
ref.clear(); // possibly already cleared by gc, nothing happens in that case
7375
++count;
7476
}
7577
_trackedRecyclers.clear(); //release cleared SoftRefs
76-
return count;
78+
} finally {
79+
RELEASE_LOCK.unlock();
7780
}
81+
return count;
7882
}
7983

8084
public SoftReference<BufferRecycler> wrapAndTrack(BufferRecycler br) {

0 commit comments

Comments
 (0)