Skip to content

Commit 4adca73

Browse files
committed
lecture
1 parent bf034e7 commit 4adca73

File tree

2 files changed

+25
-46
lines changed

2 files changed

+25
-46
lines changed

src/main/java/io/concurrency/chapter11/exam10/CompleteOnTimeoutExample.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,20 @@
66
public class CompleteOnTimeoutExample {
77
public static void main(String[] args) {
88

9-
CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
9+
getData().completeOnTimeout("Hello Java", 1, TimeUnit.SECONDS)
10+
.thenAccept(result -> {
11+
System.out.println("결과: " + result);
12+
}).join();
13+
}
14+
15+
private static CompletableFuture<String> getData() {
16+
return CompletableFuture.supplyAsync(() -> {
1017
try {
11-
TimeUnit.SECONDS.sleep(2);
18+
Thread.sleep(2000);
1219
return "Hello World";
1320
} catch (InterruptedException e) {
1421
throw new RuntimeException(e);
1522
}
1623
});
17-
18-
CompletableFuture<String> cf2 = cf
19-
.completeOnTimeout("Hello Java", 1, TimeUnit.SECONDS);
20-
21-
// 결과 처리
22-
cf2.thenAccept(result -> {
23-
System.out.println("결과: " + result);
24-
}).join();
2524
}
26-
2725
}

src/main/java/io/concurrency/chapter11/exam10/isCompletedExceptionallyAndIsCancelledExample.java

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,31 @@ public class isCompletedExceptionallyAndIsCancelledExample {
88

99
public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
1010

11-
CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(() -> {
12-
sleep(1000);
13-
return 10;
14-
});
15-
11+
CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(() -> 10);
1612
CompletableFuture<Integer> cf2 = CompletableFuture.supplyAsync(() -> {
17-
sleep(2000);
18-
return 10;
19-
// throw new RuntimeException("두 번째 작업에서 예외 발생");
20-
});
21-
22-
CompletableFuture<Integer> cf3 = CompletableFuture.supplyAsync(() -> {
23-
sleep(1000);
2413
return 20;
14+
// throw new RuntimeException("error");
2515
});
2616

2717
// cf2.cancel(true);
2818

29-
CompletableFuture<Integer> combinedFuture = cf1.thenCombine(cf2.exceptionally(e -> 15), (result1, result2) -> {
19+
CompletableFuture<Integer> combinedFuture =
20+
cf1.thenCombine(cf2.exceptionally(e -> 15), (result1, result2) -> {
3021

31-
if (cf2.isCancelled()) { // isDone() 과 구분할 수 있다
32-
return 0;
22+
if (cf2.isCancelled()) {
23+
return 0; // 취소 완료
24+
}
25+
else if (cf2.isCompletedExceptionally()) {
26+
return result2; // 예외 완료
27+
}
28+
else if(cf2.isDone()) {
29+
return result1 + result2; // 정상 완료
30+
}
31+
else return -1;
3332

34-
} else if (cf2.isCompletedExceptionally()) { // isDone() 과 구분할 수 있다
35-
return result2;
36-
37-
} else if(cf2.isDone()){
38-
return result1 + result2;
39-
} else{
40-
41-
return -1;
42-
}
43-
})
44-
.thenCombine(cf3, (result2, result3) -> result2 + result3);
33+
});
4534

4635
int result = combinedFuture.join(); // 결과 가져오기
4736
System.out.println("최종 결과: " + result);
4837
}
49-
50-
private static void sleep(int milliseconds) {
51-
try {
52-
Thread.sleep(milliseconds);
53-
} catch (InterruptedException e) {
54-
Thread.currentThread().interrupt();
55-
}
56-
}
57-
}
38+
}

0 commit comments

Comments
 (0)