Change "synchronized" to reentrant lock for virtual-threads

This commit is contained in:
Ömer Çelik
2024-10-06 01:14:36 +03:00
parent 1f2a243862
commit 5240ef3c51

View File

@@ -57,7 +57,7 @@ public class ServerlessAsyncContext implements AsyncContext {
private final List<Runnable> dispatchHandlers = new ArrayList<>(); private final List<Runnable> dispatchHandlers = new ArrayList<>();
private static final ReentrantLock globalLock = new ReentrantLock(); private final ReentrantLock globalLock = new ReentrantLock();
public ServerlessAsyncContext(ServletRequest request, @Nullable ServletResponse response) { public ServerlessAsyncContext(ServletRequest request, @Nullable ServletResponse response) {
@@ -69,7 +69,7 @@ public class ServerlessAsyncContext implements AsyncContext {
public void addDispatchHandler(Runnable handler) { public void addDispatchHandler(Runnable handler) {
Assert.notNull(handler, "Dispatch handler must not be null"); Assert.notNull(handler, "Dispatch handler must not be null");
try { try {
globalLock.lock(); this.globalLock.lock();
if (this.dispatchedPath == null) { if (this.dispatchedPath == null) {
this.dispatchHandlers.add(handler); this.dispatchHandlers.add(handler);
} }
@@ -78,7 +78,7 @@ public class ServerlessAsyncContext implements AsyncContext {
} }
} }
finally { finally {
globalLock.unlock(); this.globalLock.unlock();
} }
} }
@@ -111,12 +111,12 @@ public class ServerlessAsyncContext implements AsyncContext {
@Override @Override
public void dispatch(@Nullable ServletContext context, String path) { public void dispatch(@Nullable ServletContext context, String path) {
try { try {
globalLock.lock(); this.globalLock.lock();
this.dispatchedPath = path; this.dispatchedPath = path;
this.dispatchHandlers.forEach(Runnable::run); this.dispatchHandlers.forEach(Runnable::run);
} }
finally { finally {
globalLock.unlock(); this.globalLock.unlock();
} }
} }