File tree 3 files changed +7
-71
lines changed
src/main/java/io/concurrency/chapter11/exam05
3 files changed +7
-71
lines changed Original file line number Diff line number Diff line change @@ -7,32 +7,28 @@ public static void main(String[] args) {
7
7
8
8
MyService myService = new MyService ();
9
9
CompletableFuture <Integer > asyncFuture = CompletableFuture .supplyAsync (() -> {
10
- try {
11
- Thread .sleep (1000 );
12
- } catch (InterruptedException e ) {
13
- Thread .currentThread ().interrupt ();
14
- }
10
+ System .out .println ("thread1:" + Thread .currentThread ().getName ());
15
11
return 40 ;
16
12
})
17
13
.thenApply (result -> {
18
14
int r = myService .getData1 ();
19
- System .out .println ("thread :" + Thread .currentThread ().getName ());
15
+ System .out .println ("thread2 :" + Thread .currentThread ().getName ());
20
16
return r + result ;
21
- }).thenApplyAsync (result -> {
17
+ }).thenApply (result -> {
22
18
int r = myService .getData2 ();
23
- System .out .println ("thread :" + Thread .currentThread ().getName ());
19
+ System .out .println ("thread3 :" + Thread .currentThread ().getName ());
24
20
return r + result ;
25
21
});
26
22
27
- int asyncResult = asyncFuture .join (); // 가공된 비동기 작업 결과를 얻음
23
+ int asyncResult = asyncFuture .join ();
28
24
System .out .println ("final result: " + asyncResult );
29
25
}
30
26
31
27
static class MyService {
32
28
33
29
public int getData1 (){
34
30
try {
35
- Thread .sleep (1000 );
31
+ Thread .sleep (500 );
36
32
} catch (InterruptedException e ) {
37
33
throw new RuntimeException (e );
38
34
}
@@ -41,7 +37,7 @@ public int getData1(){
41
37
42
38
public int getData2 (){
43
39
try {
44
- Thread .sleep (1000 );
40
+ Thread .sleep (500 );
45
41
} catch (InterruptedException e ) {
46
42
throw new RuntimeException (e );
47
43
}
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments