File tree Expand file tree Collapse file tree 2 files changed +38
-40
lines changed
src/main/java/io/concurrency/chapter11/exam04 Expand file tree Collapse file tree 2 files changed +38
-40
lines changed Original file line number Diff line number Diff line change 7
7
public class RunAsyncExample {
8
8
9
9
public static void main (String [] args ) {
10
+
10
11
MyService myService = new MyService ();
11
- CompletableFuture <Void > voidFuture = CompletableFuture .runAsync (() -> {
12
- // 비동기 작업 시뮬레이션
13
- CompletableFuture <List <Integer >> future = myService .fetchDataAsync ();
14
- future .join ().stream ().forEach (System .out ::println );
15
- });
16
- System .out .println ("메인 작업 수행 중.." );
12
+ CompletableFuture .runAsync (() -> {
13
+
14
+ List <Integer > list = myService .getData ();
15
+ list .stream ().forEach (System .out ::println );
17
16
18
- voidFuture .join ();
17
+ }).join ();
18
+
19
+ System .out .println ("메인 작업 종료" );
19
20
20
21
}
21
22
22
23
static class MyService {
23
24
24
- public CompletableFuture <List <Integer >> fetchDataAsync () {
25
-
26
- return CompletableFuture .supplyAsync (() -> {
27
- // 비동기 작업 시뮬레이션
28
- List <Integer > result = new ArrayList <>();
29
- result .add (1 );
30
- result .add (2 );
31
- result .add (3 );
25
+ public List <Integer > getData () {
32
26
33
- try {
34
- Thread .sleep (1000 ); // 비동기 작업 시간 지연 시뮬레이션
35
- } catch (InterruptedException e ) {
36
- Thread .currentThread ().interrupt ();
37
- }
27
+ List <Integer > result = new ArrayList <>();
28
+ result .add (1 );
29
+ result .add (2 );
30
+ result .add (3 );
38
31
39
- return result ;
40
- });
32
+ try {
33
+ Thread .sleep (1000 ); // 비동기 작업 시간 지연 시뮬레이션
34
+ } catch (InterruptedException e ) {
35
+ Thread .currentThread ().interrupt ();
36
+ }
37
+ return result ;
41
38
}
42
39
}
43
40
}
Original file line number Diff line number Diff line change @@ -9,32 +9,33 @@ public class SupplyAsyncExample {
9
9
public static void main (String [] args ) {
10
10
11
11
MyService myService = new MyService ();
12
+ CompletableFuture .supplyAsync (() -> {
12
13
13
- CompletableFuture <List <Integer >> future = myService .fetchDataAsync ();
14
- System .out .println ("메인 작업 수행 중.." );
15
- future .join ().stream ().forEach (System .out ::println );
14
+ List <Integer > list = myService .getData ();
15
+ list .stream ().forEach (System .out ::println );
16
+ return list ;
17
+
18
+ }).join ();
19
+
20
+ System .out .println ("메인 작업 종료" );
16
21
17
22
}
18
23
19
24
static class MyService {
20
25
21
- public CompletableFuture <List <Integer >> fetchDataAsync () {
22
-
23
- return CompletableFuture .supplyAsync (() -> {
24
- // 비동기 작업 시뮬레이션
25
- List <Integer > result = new ArrayList <>();
26
- result .add (1 );
27
- result .add (2 );
28
- result .add (3 );
26
+ public List <Integer > getData () {
29
27
30
- try {
31
- Thread .sleep (1000 ); // 비동기 작업 시간 지연 시뮬레이션
32
- } catch (InterruptedException e ) {
33
- Thread .currentThread ().interrupt ();
34
- }
28
+ List <Integer > result = new ArrayList <>();
29
+ result .add (1 );
30
+ result .add (2 );
31
+ result .add (3 );
35
32
36
- return result ;
37
- });
33
+ try {
34
+ Thread .sleep (1000 );
35
+ } catch (InterruptedException e ) {
36
+ Thread .currentThread ().interrupt ();
37
+ }
38
+ return result ;
38
39
}
39
40
}
40
41
}
You can’t perform that action at this time.
0 commit comments