diff --git a/vavr/src/main/java/io/vavr/concurrent/Future.java b/vavr/src/main/java/io/vavr/concurrent/Future.java index 59b3d3f5c..d5546ef88 100644 --- a/vavr/src/main/java/io/vavr/concurrent/Future.java +++ b/vavr/src/main/java/io/vavr/concurrent/Future.java @@ -32,6 +32,8 @@ import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.function.*; /** @@ -178,8 +180,10 @@ static Future> find(Executor executor, Iterable { final AtomicBoolean completed = new AtomicBoolean(false); final AtomicInteger count = new AtomicInteger(list.length()); + final Lock lock = new ReentrantLock(); list.forEach(future -> future.onComplete(result -> { - synchronized (count) { + lock.lock(); + try { // if the future is already completed we already found our result and there is nothing more to do. if (!completed.get()) { // when there are no more results we return a None @@ -193,6 +197,8 @@ static Future> find(Executor executor, Iterable