Skip to content

Commit 999d6b2

Browse files
committed
lecture
1 parent bd7d759 commit 999d6b2

File tree

3 files changed

+39
-52
lines changed

3 files changed

+39
-52
lines changed
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
11
package io.concurrency.chapter11.exam09;
22

3-
43
import java.util.concurrent.CompletableFuture;
54

65
public class ExceptionallyExample {
76
public static void main(String[] args) {
87

9-
10-
CompletableFuture<Object> cf = CompletableFuture.supplyAsync(() -> {
8+
CompletableFuture<Integer> cf = CompletableFuture.supplyAsync(() -> {
119
try {
1210
Thread.sleep(500);
13-
throw new RuntimeException("error");
14-
11+
throw new IllegalArgumentException("error");
1512
} catch (InterruptedException e) {
1613
Thread.currentThread().interrupt();
1714
}
1815
return 10;
19-
})
20-
.thenApply(r -> r + 20)
21-
.exceptionally(ex -> {
22-
System.err.println("최종 예외 처리: " + ex.getMessage());
23-
return -1;
24-
}).thenApply(r -> {
25-
throw new IllegalArgumentException("error");
26-
}).exceptionally(ex -> {
27-
System.err.println("최종 예외 처리: " + ex.getMessage());
16+
}).thenApply(r -> r + 20)
17+
.exceptionally(e -> {
18+
System.out.println("Exception: " + e.getMessage());
2819
return -1;
2920
});
3021

31-
System.out.println("result: " + cf.join());
22+
System.out.println("result: " +cf.join());
3223
}
3324
}

src/main/java/io/concurrency/chapter11/exam09/HandleExample.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,32 @@ public static void main(String[] args) {
1212
Thread.currentThread().interrupt();
1313
}
1414
return 10;
15-
})
16-
.handle((r, e) -> {
17-
if (e != null) {
18-
System.err.println("비동기 예외 처리 1: " + e.getMessage());
19-
return -1; // 예외 발생 시 기본값 반환
15+
}).handle((r, e) ->{
16+
if(e !=null){
17+
System.out.println("비동기 예외처리 1: " + e.getMessage());
18+
return -1;
2019
}
2120
return r;
2221
});
2322

2423
CompletableFuture<Integer> cf2 = CompletableFuture.supplyAsync(() -> {
2524
try {
2625
Thread.sleep(500);
27-
// throw new RuntimeException("error");
26+
throw new RuntimeException("error");
2827

2928
} catch (InterruptedException e) {
3029
Thread.currentThread().interrupt();
3130
}
3231
return 20;
33-
})
34-
.handle((r, e) -> {
35-
if (e != null) {
36-
System.err.println("비동기 예외 처리 2: " + e.getMessage());
37-
return -1; // 예외 발생 시 기본값 반환
32+
}).handle((r, e) ->{
33+
if(e !=null){
34+
System.out.println("비동기 예외처리 2: " + e.getMessage());
35+
return -1;
3836
}
3937
return r;
4038
});
4139

40+
4241
CompletableFuture<Integer> cf3 = cf1.thenCombine(cf2, (r1, r2) -> {
4342
if (r1 == -1 || r2 == -1) {
4443
// 둘 중 하나라도 예외가 발생하면 예외 처리
Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
11
package io.concurrency.chapter11.exam09;
22

33
import java.util.concurrent.CompletableFuture;
4-
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.CompletionException;
55

66
public class WhenCompleteExample {
7-
public static void main(String[] args) {
7+
public static void main(String[] args) throws InterruptedException {
88

9-
CompletableFuture<Integer> cf = CompletableFuture.supplyAsync(() -> {
10-
try {
11-
Thread.sleep(500);
12-
throw new RuntimeException("error");
13-
} catch (InterruptedException e) {
14-
Thread.currentThread().interrupt();
15-
}
16-
return 10;
17-
})
18-
.whenComplete((r, e) -> {
19-
if (e != null) {
20-
System.err.println("예외 발생: " + e.getMessage());
21-
// 예외 처리
22-
} else {
23-
// 결과 처리
24-
System.out.println("결과: " + r);
25-
}
26-
});
9+
CompletableFuture<Integer> cf = CompletableFuture.supplyAsync(() -> {
10+
try {
11+
Thread.sleep(500);
12+
throw new RuntimeException("error");
13+
} catch (InterruptedException e) {
14+
Thread.currentThread().interrupt();
15+
}
16+
return 10;
17+
}).whenComplete((r,e) ->{
18+
if(e != null){
19+
System.out.println("Exception: " + e.getMessage());
20+
}else{
21+
System.out.println("result: " + r);
22+
}
23+
});
2724

2825
try {
29-
cf.get();
30-
} catch (InterruptedException ex) {
31-
System.out.println("예외 처리: " + ex.getMessage());
32-
33-
} catch (ExecutionException ex) {
34-
System.out.println("예외 처리: " + ex.getMessage());
26+
Thread.sleep(2000);
27+
cf.join();
28+
}catch(CompletionException e){
29+
System.out.println("예외 처리를 합니다");
30+
} catch (InterruptedException e) {
31+
throw new RuntimeException(e);
3532
}
3633
}
3734
}

0 commit comments

Comments
 (0)