Skip to content

Commit 95c9423

Browse files
committed
lecture
1 parent 65f1940 commit 95c9423

File tree

6 files changed

+38
-79
lines changed

6 files changed

+38
-79
lines changed

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,36 @@
33
import java.util.concurrent.CompletableFuture;
44
import java.util.concurrent.ExecutorService;
55
import java.util.concurrent.Executors;
6-
import java.util.concurrent.TimeUnit;
76

87
public class CompleteExample {
98
public static void main(String[] args) {
109

11-
MyService myService = new MyService();
10+
MyService service = new MyService();
11+
CompletableFuture<Integer> cf = service.performTask();
12+
cf.thenApply(r -> r + 20);
1213

13-
CompletableFuture<Integer> future = myService.performTask();
14+
System.out.println("result: " + cf.join());
15+
System.out.println("메인 스레드 종료");
1416

15-
future.thenAccept(result -> {
16-
System.out.println("비동기 작업 결과: " + result);
17-
});
1817
}
1918

20-
static class MyService {
19+
static class MyService{
2120

22-
public CompletableFuture<Integer> performTask() {
21+
public CompletableFuture<Integer> performTask(){
22+
23+
CompletableFuture<Integer> cf = new CompletableFuture<>();
2324

2425
ExecutorService executorService = Executors.newSingleThreadExecutor();
25-
CompletableFuture<Integer> future = new CompletableFuture<>();
26+
executorService.submit(()->{
2627

27-
executorService.submit(() -> {
2828
try {
29-
TimeUnit.SECONDS.sleep(2);
30-
int result = 42;
31-
future.complete(result); // 결과를 완료시킴
32-
} catch (InterruptedException e) {}
29+
Thread.sleep(1000);
30+
} catch (InterruptedException e) {
31+
throw new RuntimeException(e);
32+
}
33+
cf.complete(40);
3334
});
34-
35-
return future;
35+
return cf;
3636
}
3737
}
38-
}
38+
}

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

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

9-
getData().completeOnTimeout("Hello Java", 1, TimeUnit.SECONDS)
10-
.thenAccept(result -> {
11-
System.out.println("결과: " + result);
12-
}).join();
9+
getData().completeOnTimeout("Hello Java", 2, TimeUnit.SECONDS)
10+
.thenAccept(r -> {
11+
System.out.println("r = " + r);
12+
}).join();
1313
}
1414

1515
private static CompletableFuture<String> getData() {
16-
return CompletableFuture.supplyAsync(() -> {
16+
17+
return CompletableFuture.supplyAsync(()->{
1718
try {
1819
Thread.sleep(2000);
1920
return "Hello World";

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
public class CompletedFutureExample {
66
public static void main(String[] args) {
77

8-
CompletableFuture<String> cf1 = CompletableFuture.completedFuture("Hello World");
8+
CompletableFuture<String> cf = CompletableFuture.completedFuture("Hello World");
99

10-
CompletableFuture<String> cf2 = new CompletableFuture<>();
11-
cf2.complete("Hello World");
10+
// CompletableFuture<String> cf2 = new CompletableFuture<>();
11+
// cf2.complete("Hello World");
1212

13-
cf1.thenAccept(user -> {
14-
System.out.println("결과: " + user);
13+
CompletableFuture<Void> finalCf = cf.thenAccept(r -> {
14+
System.out.println("result: " + r);
1515
});
16-
1716
}
1817
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public static void main(String[] args) {
2323
System.out.println("result: " + cf2.join());
2424
}
2525

26-
private static void getData(CompletableFuture<String> cf1) {
26+
private static void getData(CompletableFuture cf) {
2727
try {
28-
System.out.println("비동기 작업 수행중");
29-
Thread.sleep(100);
30-
throw new IllegalArgumentException("error");
28+
System.out.println("비동기 작업 수행 중..");
29+
Thread.sleep(500);
30+
// throw new IllegalArgumentException("error");
3131
} catch (Exception e) {
32-
cf1.completeExceptionally(e);
32+
cf.completeExceptionally(e);
3333
}
34-
cf1.complete("Hello World");
34+
cf.complete("Hello World");
3535
}
36-
}
36+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public static void main(String[] args) throws InterruptedException, ExecutionExc
1010

1111
CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(() -> 10);
1212
CompletableFuture<Integer> cf2 = CompletableFuture.supplyAsync(() -> {
13-
return 20;
14-
// throw new RuntimeException("error");
13+
// return 20;
14+
throw new RuntimeException("error");
1515
});
1616

1717
// cf2.cancel(true);
@@ -35,4 +35,4 @@ else if(cf2.isDone()) {
3535
int result = combinedFuture.join(); // 결과 가져오기
3636
System.out.println("최종 결과: " + result);
3737
}
38-
}
38+
}

src/main/java/io/concurrency/chapter11/exam11/CompletableFutureCancelExample.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)