Skip to content

Commit 100554b

Browse files
Koziolekpivovarit
authored andcommitted
Replace synchronized blocks with virtual-thread-friendly Lock in Future.find() (#2910)
I replaced synchronized blocks with `Lock` because of virtual threads. More details: https://docs.oracle.com/en/java/javase/21/core/virtual-threads.html#GUID-04C03FFC-066D-4857-85B9-E5A27A875AF9 Similar to #2845
1 parent 8a44acb commit 100554b

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

vavr/src/main/java/io/vavr/concurrent/Future.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.util.concurrent.*;
3333
import java.util.concurrent.atomic.AtomicBoolean;
3434
import java.util.concurrent.atomic.AtomicInteger;
35+
import java.util.concurrent.locks.Lock;
36+
import java.util.concurrent.locks.ReentrantLock;
3537
import java.util.function.*;
3638

3739
/**
@@ -178,8 +180,10 @@ static <T> Future<Option<T>> find(Executor executor, Iterable<? extends Future<?
178180
return run(executor, complete -> {
179181
final AtomicBoolean completed = new AtomicBoolean(false);
180182
final AtomicInteger count = new AtomicInteger(list.length());
183+
final Lock lock = new ReentrantLock();
181184
list.forEach(future -> future.onComplete(result -> {
182-
synchronized (count) {
185+
lock.lock();
186+
try {
183187
// if the future is already completed we already found our result and there is nothing more to do.
184188
if (!completed.get()) {
185189
// when there are no more results we return a None
@@ -193,6 +197,8 @@ static <T> Future<Option<T>> find(Executor executor, Iterable<? extends Future<?
193197
}
194198
});
195199
}
200+
} finally {
201+
lock.unlock();
196202
}
197203
}));
198204
});

0 commit comments

Comments
 (0)