Skip to content

Commit 9ae36c7

Browse files
committed
lecture
1 parent 5686c67 commit 9ae36c7

File tree

3 files changed

+51
-44
lines changed

3 files changed

+51
-44
lines changed

src/main/java/io/concurrency/chapter08/exam03/ReadWriteLockReadLockExample.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public static void main(String[] args) {
99

1010
ReadWriteLock lock = new ReentrantReadWriteLock();
1111

12+
13+
1214
BankAccount account = new BankAccount(lock, 0);
1315

1416
// 읽기 스레드가 잔액 조회
Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
11
package io.concurrency.chapter11.exam04;
22

3-
import java.util.ArrayList;
3+
import java.util.Arrays;
44
import java.util.List;
55
import java.util.concurrent.CompletableFuture;
66

77
public class RunAsyncExample {
88
public static void main(String[] args) {
99

10-
MyService myService = new MyService();
10+
MyService2 service = new MyService2();
1111

12-
CompletableFuture.runAsync(() -> {
12+
CompletableFuture<Void> cf = CompletableFuture.runAsync(() -> {
13+
System.out.println(Thread.currentThread().getName() + " 가 비동기 작업을 시작합니다");
14+
service.getData().stream().forEach(System.out::println);
15+
});
1316

14-
System.out.println(Thread.currentThread().getName() + " 가 비동기 작업을 시작 합니다.");
15-
List<Integer> result = myService.getData();
16-
17-
result.stream().forEach(System.out::println);
18-
19-
}).join();
17+
cf.join();
18+
System.out.println("메인 스레드 종료");
2019
}
20+
}
2121

22-
static class MyService {
23-
24-
public List<Integer> getData() {
25-
26-
List<Integer> result = new ArrayList<>();
27-
result.add(1);
28-
result.add(2);
29-
result.add(3);
22+
class MyService2{
3023

31-
try {
32-
Thread.sleep(1000);
33-
} catch (InterruptedException e) {
34-
Thread.currentThread().interrupt();
35-
}
36-
return result;
24+
public List<Integer> getData(){
25+
try {
26+
Thread.sleep(1000);
27+
} catch (InterruptedException e) {
28+
throw new RuntimeException(e);
3729
}
30+
return Arrays.asList(1,2,3);
3831
}
3932
}
Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,51 @@
11
package io.concurrency.chapter11.exam04;
22

3-
import java.util.ArrayList;
3+
import java.util.Arrays;
44
import java.util.List;
5-
import java.util.concurrent.CompletableFuture;
5+
import java.util.concurrent.*;
66

77
public class SupplyAsyncExample {
88
public static void main(String[] args) {
99

10-
MyService myService = new MyService();
10+
MyService service = new MyService();
1111

12-
List<Integer> result = CompletableFuture.supplyAsync(() -> {
12+
CompletableFuture<List<Integer>> cf = CompletableFuture.supplyAsync(() -> {
13+
System.out.println(Thread.currentThread().getName() + " 가 비동기 작업을 시작합니다");
14+
return service.getData();
15+
});
1316

14-
System.out.println(Thread.currentThread().getName() + " 가 비동기 작업을 시작 합니다.");
15-
return myService.getData();
17+
List<Integer> result = cf.join();
18+
result.stream().forEach(r->System.out.println(r));
1619

17-
}).join();
20+
System.out.println("===============================================================================");
1821

19-
result.stream().forEach(System.out::println);
20-
}
22+
ExecutorService executorService = Executors.newFixedThreadPool(1);
23+
Future<List<Integer>> future = executorService.submit(() -> {
24+
System.out.println(Thread.currentThread().getName() + " 가 비동기 작업을 시작합니다");
25+
return service.getData();
26+
});
2127

22-
static class MyService {
28+
try {
29+
List<Integer> result2 = future.get();
30+
result2.stream().forEach(r->System.out.println(r));
31+
} catch (InterruptedException e) {
32+
throw new RuntimeException(e);
33+
} catch (ExecutionException e) {
34+
throw new RuntimeException(e);
35+
}
2336

24-
public List<Integer> getData() {
37+
System.out.println("메인 스레드 종료");
38+
}
39+
}
2540

26-
List<Integer> result = new ArrayList<>();
27-
result.add(1);
28-
result.add(2);
29-
result.add(3);
41+
class MyService{
3042

31-
try {
32-
Thread.sleep(1000);
33-
} catch (InterruptedException e) {
34-
Thread.currentThread().interrupt();
35-
}
36-
return result;
43+
public List<Integer> getData(){
44+
try {
45+
Thread.sleep(1000);
46+
} catch (InterruptedException e) {
47+
throw new RuntimeException(e);
3748
}
49+
return Arrays.asList(1,2,3);
3850
}
3951
}

0 commit comments

Comments
 (0)