3
3
import java .util .concurrent .CompletableFuture ;
4
4
5
5
public class AllOfExample {
6
- public static void main (String [] args ) {
6
+ public static void main (String [] args ) throws InterruptedException {
7
7
8
8
ServiceA sa = new ServiceA ();
9
9
ServiceB sb = new ServiceB ();
10
10
ServiceC sc = new ServiceC ();
11
11
12
- CompletableFuture <Integer > futureA = sa .fetchAsyncDataA ();
13
- CompletableFuture <Integer > futureB = sb .fetchAsyncDataB ();
14
- CompletableFuture <Integer > futureC = sc .fetchAsyncDataC ();
12
+ CompletableFuture <Integer > cf1 = sa .getData1 ();
13
+ CompletableFuture <Integer > cf2 = sb .getData2 ();
14
+ CompletableFuture <Integer > cf3 = sc .getData3 ();
15
15
16
- CompletableFuture <Integer > cf = CompletableFuture .allOf (futureA , futureB , futureC )
17
- .thenApplyAsync (v -> {
18
- int resultA = futureA .join ();
19
- int resultB = futureB .join ();
20
- int resultC = futureC .join ();
21
- return resultA + resultB + resultC ;
22
- });
16
+ long started = System .currentTimeMillis ();
17
+ CompletableFuture <Void > voidCf = CompletableFuture .allOf (cf1 , cf2 , cf3 );
18
+ CompletableFuture <Integer > finalCf = voidCf .thenApply (v -> {
23
19
24
- System .out .println ("result: " + cf .join ());
20
+ int result1 = cf1 .join ();
21
+ int result2 = cf2 .join ();
22
+ int result3 = cf3 .join ();
23
+
24
+ System .out .println ("result1 = " + result1 );
25
+ System .out .println ("result2 = " + result2 );
26
+ System .out .println ("result3 = " + result3 );
27
+
28
+ return result1 + result2 + result3 ;
29
+
30
+ });
31
+ // Thread.sleep(2000);
32
+ finalCf .join ();
33
+ System .out .println ("최종 소요 시간: " + (System .currentTimeMillis () - started ));
34
+
35
+ // System.out.println("최종결과: " + voidCf);
36
+ System .out .println ("최종결과: " + finalCf .join ());
37
+ System .out .println ("메인 스레드 종료" );
25
38
}
26
39
27
40
static class ServiceA {
28
41
29
- public CompletableFuture <Integer > fetchAsyncDataA () {
42
+ public CompletableFuture <Integer > getData1 () {
30
43
// 비동기 작업 시뮬레이션
31
44
return CompletableFuture .supplyAsync (() -> {
32
45
try {
33
- Thread .sleep (500 );
46
+ Thread .sleep (1000 );
47
+ System .out .println ("비동기 작업 시작 1" );
34
48
} catch (InterruptedException e ) {
35
49
Thread .currentThread ().interrupt ();
36
50
}
@@ -41,11 +55,12 @@ public CompletableFuture<Integer> fetchAsyncDataA() {
41
55
42
56
static class ServiceB {
43
57
44
- public CompletableFuture <Integer > fetchAsyncDataB () {
58
+ public CompletableFuture <Integer > getData2 () {
45
59
// 비동기 작업 시뮬레이션
46
60
return CompletableFuture .supplyAsync (() -> {
47
61
try {
48
- Thread .sleep (400 );
62
+ Thread .sleep (2000 );
63
+ System .out .println ("비동기 작업 시작 2" );
49
64
} catch (InterruptedException e ) {
50
65
Thread .currentThread ().interrupt ();
51
66
}
@@ -56,11 +71,12 @@ public CompletableFuture<Integer> fetchAsyncDataB() {
56
71
57
72
static class ServiceC {
58
73
59
- public CompletableFuture <Integer > fetchAsyncDataC () {
74
+ public CompletableFuture <Integer > getData3 () {
60
75
// 비동기 작업 시뮬레이션
61
76
return CompletableFuture .supplyAsync (() -> {
62
77
try {
63
- Thread .sleep (300 );
78
+ Thread .sleep (1000 );
79
+ System .out .println ("비동기 작업 시작 3" );
64
80
} catch (InterruptedException e ) {
65
81
Thread .currentThread ().interrupt ();
66
82
}
0 commit comments