Skip to content

Commit 180d55b

Browse files
committed
Remove streams for AsyncCombinedLock
1 parent 926f329 commit 180d55b

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/main/java/org/yatopiamc/c2me/common/util/AsyncCombinedLock.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,21 @@ private synchronized void tryAcquire() { // TODO optimize logic
4242
if (allAcquired) {
4343
future.complete(new CombinedLockToken(tryLocks.stream().flatMap(lockEntry -> lockEntry.lockToken.stream()).collect(Collectors.toUnmodifiableSet())));
4444
} else {
45-
tryLocks.stream().flatMap(lockEntry -> lockEntry.lockToken.stream()).forEach(AsyncLock.LockToken::releaseLock);
46-
tryLocks.stream().unordered().filter(lockEntry -> lockEntry.lockToken.isEmpty()).findFirst().ifPresentOrElse(lockEntry ->
47-
lockEntry.lock.acquireLock().thenCompose(lockToken -> {
45+
boolean triedRelock = false;
46+
for (LockEntry entry : tryLocks) {
47+
entry.lockToken.ifPresent(AsyncLock.LockToken::releaseLock);
48+
if (!triedRelock && entry.lockToken.isEmpty()) {
49+
entry.lock.acquireLock().thenCompose(lockToken -> {
4850
lockToken.releaseLock();
4951
return CompletableFuture.runAsync(this::tryAcquire, lockWorker);
50-
}), this::tryAcquire);
52+
});
53+
triedRelock = true;
54+
}
55+
}
56+
if (!triedRelock) {
57+
// shouldn't happen at all...
58+
lockWorker.execute(this::tryAcquire);
59+
}
5160
}
5261
}
5362

0 commit comments

Comments
 (0)