File tree 2 files changed +11
-14
lines changed
src/main/java/io/concurrency/chapter11/exam04
2 files changed +11
-14
lines changed Original file line number Diff line number Diff line change 5
5
import java .util .concurrent .CompletableFuture ;
6
6
7
7
public class RunAsyncExample {
8
-
9
8
public static void main (String [] args ) {
10
9
11
10
MyService myService = new MyService ();
11
+
12
12
CompletableFuture .runAsync (() -> {
13
13
14
- List < Integer > list = myService . getData ( );
15
- list . stream (). forEach ( System . out :: println );
14
+ System . out . println ( Thread . currentThread (). getName () + " 가 비동기 작업을 시작 합니다." );
15
+ List < Integer > result = myService . getData ( );
16
16
17
- }).join ();
18
-
19
- System .out .println ("메인 작업 종료" );
17
+ result .stream ().forEach (System .out ::println );
20
18
19
+ }).join ();
21
20
}
22
21
23
22
static class MyService {
@@ -30,7 +29,7 @@ public List<Integer> getData() {
30
29
result .add (3 );
31
30
32
31
try {
33
- Thread .sleep (1000 ); // 비동기 작업 시간 지연 시뮬레이션
32
+ Thread .sleep (1000 );
34
33
} catch (InterruptedException e ) {
35
34
Thread .currentThread ().interrupt ();
36
35
}
Original file line number Diff line number Diff line change 5
5
import java .util .concurrent .CompletableFuture ;
6
6
7
7
public class SupplyAsyncExample {
8
-
9
8
public static void main (String [] args ) {
10
9
11
10
MyService myService = new MyService ();
12
- CompletableFuture .supplyAsync (() -> {
13
11
14
- List <Integer > list = myService .getData ();
15
- list .stream ().forEach (System .out ::println );
16
- return list ;
12
+ List <Integer > result = CompletableFuture .supplyAsync (() -> {
17
13
18
- }).join ();
14
+ System .out .println (Thread .currentThread ().getName () + " 가 비동기 작업을 시작 합니다." );
15
+ return myService .getData ();
19
16
20
- System . out . println ( "메인 작업 종료" );
17
+ }). join ( );
21
18
19
+ result .stream ().forEach (System .out ::println );
22
20
}
23
21
24
22
static class MyService {
You can’t perform that action at this time.
0 commit comments