Skip to content

Commit 0b09f1e

Browse files
committed
Use ReentrantLock instead of synchronization for concurrency throttle
Closes gh-32251
1 parent 120ea0a commit 0b09f1e

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

spring-core/src/main/java/org/springframework/util/ConcurrencyThrottleSupport.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,9 @@
1919
import java.io.IOException;
2020
import java.io.ObjectInputStream;
2121
import java.io.Serializable;
22+
import java.util.concurrent.locks.Condition;
23+
import java.util.concurrent.locks.Lock;
24+
import java.util.concurrent.locks.ReentrantLock;
2225

2326
import org.apache.commons.logging.Log;
2427
import org.apache.commons.logging.LogFactory;
@@ -60,7 +63,9 @@ public abstract class ConcurrencyThrottleSupport implements Serializable {
6063
/** Transient to optimize serialization. */
6164
protected transient Log logger = LogFactory.getLog(getClass());
6265

63-
private transient Object monitor = new Object();
66+
private final Lock concurrencyLock = new ReentrantLock();
67+
68+
private final Condition concurrencyCondition = this.concurrencyLock.newCondition();
6469

6570
private int concurrencyLimit = UNBOUNDED_CONCURRENCY;
6671

@@ -109,7 +114,8 @@ protected void beforeAccess() {
109114
}
110115
if (this.concurrencyLimit > 0) {
111116
boolean debug = logger.isDebugEnabled();
112-
synchronized (this.monitor) {
117+
this.concurrencyLock.lock();
118+
try {
113119
boolean interrupted = false;
114120
while (this.concurrencyCount >= this.concurrencyLimit) {
115121
if (interrupted) {
@@ -121,7 +127,7 @@ protected void beforeAccess() {
121127
" has reached limit " + this.concurrencyLimit + " - blocking");
122128
}
123129
try {
124-
this.monitor.wait();
130+
this.concurrencyCondition.await();
125131
}
126132
catch (InterruptedException ex) {
127133
// Re-interrupt current thread, to allow other threads to react.
@@ -134,6 +140,9 @@ protected void beforeAccess() {
134140
}
135141
this.concurrencyCount++;
136142
}
143+
finally {
144+
this.concurrencyLock.unlock();
145+
}
137146
}
138147
}
139148

@@ -144,12 +153,16 @@ protected void beforeAccess() {
144153
protected void afterAccess() {
145154
if (this.concurrencyLimit >= 0) {
146155
boolean debug = logger.isDebugEnabled();
147-
synchronized (this.monitor) {
156+
this.concurrencyLock.lock();
157+
try {
148158
this.concurrencyCount--;
149159
if (debug) {
150160
logger.debug("Returning from throttle at concurrency count " + this.concurrencyCount);
151161
}
152-
this.monitor.notify();
162+
this.concurrencyCondition.signal();
163+
}
164+
finally {
165+
this.concurrencyLock.unlock();
153166
}
154167
}
155168
}
@@ -165,7 +178,6 @@ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFound
165178

166179
// Initialize transient fields.
167180
this.logger = LogFactory.getLog(getClass());
168-
this.monitor = new Object();
169181
}
170182

171183
}

0 commit comments

Comments
 (0)