Update FutureLockUtils.java

This commit is contained in:
Andrea Cavalli 2020-06-29 12:07:29 +02:00
parent 6e2c0feddf
commit 27ec2c3a40

View File

@ -17,11 +17,18 @@ public class FutureLockUtils {
} else { } else {
lockValue = 0; lockValue = 0;
} }
return r.get().whenComplete((x, y) -> { try {
return r.get().whenComplete((x, y) -> {
if (lock != null) {
lock.unlockRead(lockValue);
}
});
} catch (Throwable ex) {
if (lock != null) { if (lock != null) {
lock.unlockRead(lockValue); lock.unlockRead(lockValue);
} }
}); throw ex;
}
} }
public static <T> CompletableFuture<T> writeLock(@Nullable StampedLock lock, @NotNull Supplier<CompletableFuture<T>> r) { public static <T> CompletableFuture<T> writeLock(@Nullable StampedLock lock, @NotNull Supplier<CompletableFuture<T>> r) {
@ -31,11 +38,18 @@ public class FutureLockUtils {
} else { } else {
lockValue = 0; lockValue = 0;
} }
return r.get().whenComplete((x, y) -> { try {
return r.get().whenComplete((x, y) -> {
if (lock != null) {
lock.unlockWrite(lockValue);
}
});
} catch (Throwable ex) {
if (lock != null) { if (lock != null) {
lock.unlockWrite(lockValue); lock.unlockWrite(lockValue);
} }
}); throw ex;
}
} }
public static <T> CompletableFuture<T> readLockIO(@Nullable StampedLock lock, @NotNull IOSupplier<CompletableFuture<T>> r) throws IOException { public static <T> CompletableFuture<T> readLockIO(@Nullable StampedLock lock, @NotNull IOSupplier<CompletableFuture<T>> r) throws IOException {
@ -45,11 +59,18 @@ public class FutureLockUtils {
} else { } else {
lockValue = 0; lockValue = 0;
} }
return r.get().whenComplete((x, y) -> { try {
return r.get().whenComplete((x, y) -> {
if (lock != null) {
lock.unlockRead(lockValue);
}
});
} catch (Throwable ex) {
if (lock != null) { if (lock != null) {
lock.unlockRead(lockValue); lock.unlockRead(lockValue);
} }
}); throw ex;
}
} }
public static <T> CompletableFuture<T> writeLockIO(@Nullable StampedLock lock, @NotNull IOSupplier<CompletableFuture<T>> r) throws IOException { public static <T> CompletableFuture<T> writeLockIO(@Nullable StampedLock lock, @NotNull IOSupplier<CompletableFuture<T>> r) throws IOException {
@ -59,10 +80,17 @@ public class FutureLockUtils {
} else { } else {
lockValue = 0; lockValue = 0;
} }
return r.get().whenComplete((x, y) -> { try {
return r.get().whenComplete((x, y) -> {
if (lock != null) {
lock.unlockWrite(lockValue);
}
});
} catch (Throwable ex) {
if (lock != null) { if (lock != null) {
lock.unlockWrite(lockValue); lock.unlockWrite(lockValue);
} }
}); throw ex;
}
} }
} }