Skip to content

Commit 5d7f0cc

Browse files
committed
LinkedCompletion - cancel major (dependency) future first
1 parent d99f6ff commit 5d7f0cc

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/main/java/net/tascalate/concurrent/LinkedCompletion.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ LinkedCompletion<T, F> dependsOn(F dependency) {
3131

3232
@Override
3333
public boolean cancel(boolean mayInterruptIfRunning) {
34-
if (super.cancel(mayInterruptIfRunning)) {
35-
cancelDependency(mayInterruptIfRunning);
36-
return true;
34+
if (cancelDependency(mayInterruptIfRunning)) {
35+
return super.cancel(mayInterruptIfRunning);
3736
} else {
3837
return false;
3938
}

src/test/java/net/tascalate/concurrent/J8Examples.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@
2727
public class J8Examples {
2828

2929
public static void main(final String[] argv) throws InterruptedException, ExecutionException {
30-
System.out.println( Duration.ofNanos(Long.MAX_VALUE).toDays() );
31-
System.out.println( Duration.ofNanos(Long.MAX_VALUE) );
32-
3330
final TaskExecutorService executorService = TaskExecutors.newFixedThreadPool(3);
34-
31+
32+
final Promise<Number> p = Promises.any(
33+
CompletableTask.supplyAsync(() -> awaitAndProduce1(20, 100), executorService),
34+
CompletableTask.supplyAsync(() -> awaitAndProduce1(-10, 50), executorService)
35+
);
36+
p.whenComplete((r,e) -> {
37+
System.out.println("Result = " + r + ", Error = " + e);
38+
});
39+
//p.cancel(true);
40+
3541
Promise<String> retry1 = Promises.poll(
3642
() -> "ABC",
3743
executorService, RetryPolicy.DEFAULT
@@ -101,7 +107,9 @@ public static void main(final String[] argv) throws InterruptedException, Execut
101107
executorService.submit(() -> awaitAndProduceN(7)),
102108
executorService.submit(() -> awaitAndProduceN(8)),
103109
executorService.submit(() -> awaitAndProduceN(11))
104-
).thenApplyAsync(
110+
)
111+
.defaultAsyncOn(executorService)
112+
.thenApplyAsync(
105113
l -> l.stream().filter(v -> v != null).collect(Collectors.summingInt((Integer i) -> i.intValue()))
106114
)
107115
.thenAcceptAsync(J8Examples::onComplete)

0 commit comments

Comments
 (0)