Skip to content

Commit c35125f

Browse files
committed
join()
1 parent 83dc6cb commit c35125f

File tree

4 files changed

+106
-13
lines changed

4 files changed

+106
-13
lines changed

src/main/java/io/concurrency/chapter03/exam02/BasicJoinExample.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
public class BasicJoinExample {
44

5-
public static void main(String[] args) {
5+
public static void main(String[] args) { //메인 함수의 메인 스레드가 일종의 부모 스레드
66

7-
Thread thread = new Thread(() -> {
7+
Thread thread = new Thread(() -> { //메인 스레드의 자식 스레드
88
try {
99
System.out.println("스레드가 3초 동안 작동합니다.");
1010
Thread.sleep(5000);
@@ -18,14 +18,16 @@ public static void main(String[] args) {
1818

1919
System.out.println("메인 스레드가 다른 스레드의 완료를 기다립니다.");
2020

21-
try {
2221

23-
thread.join();
22+
try {
23+
//메인 스레드가 대상(자식) 스레드에게 join()을 걸고 있다!
24+
thread.join(); //자식 스레드가 완료될 때까지 대기
2425

2526
} catch (InterruptedException e) {
2627
throw new RuntimeException(e);
2728
}
2829

2930
System.out.println("메인 스레드가 계속 진행합니다");
31+
3032
}
3133
}

src/main/java/io/concurrency/chapter03/exam02/InterruptedJoinExample.java

+48-9
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ public static void main(String[] args) {
99
Thread longRunningThread = new Thread(() -> {
1010
try {
1111
for (int i = 0; i < 10; i++) {
12-
System.out.println("긴 작업 스레드가 계속 실행 중...");
12+
System.out.println("긴 스레드가 계속 실행 중...");
1313
Thread.sleep(1000);
1414
}
1515
} catch (InterruptedException e) {
1616
mainThread.interrupt();
17-
System.out.println("긴 작업 스레드가 인터럽트 되었습니다.");
17+
System.out.println("긴 작업 스레드가 인터럽트 되었습니다");
1818
}
1919
});
2020

2121
longRunningThread.start();
2222

2323
Thread interruptingThread = new Thread(() -> {
2424
try {
25-
System.out.println("인터럽트 스레드가 2초 후에 긴 작업 스레드를 인터럽트 합니다.");
25+
System.out.println("인터럽트 스레드가 2초 후에 긴 작업 스레드를 인터럽트 합니다");
2626
Thread.sleep(2000);
2727
longRunningThread.interrupt();
2828
} catch (InterruptedException e) {
@@ -32,15 +32,54 @@ public static void main(String[] args) {
3232

3333
interruptingThread.start();
3434

35-
36-
try {
35+
//메인 스레드가 조인에서 빠져나오는 것을 확인하자
36+
//즉 메인 스레드에게 인터럽트를 걸어야 한다!!
37+
try{
3738
System.out.println("메인 스레드가 긴 작업 스레드의 완료를 기다립니다.");
3839
longRunningThread.join();
3940
System.out.println("메인 스레드 작업 완료");
40-
41-
} catch (InterruptedException e) {
42-
System.out.println("메인 스레드가 인터럽트 되었습니다.");
43-
throw new RuntimeException(e);
41+
}catch (InterruptedException e){
42+
System.out.println("메인 스레드가 인터럽트 되었습니다");
4443
}
44+
45+
46+
// Thread mainThread = Thread.currentThread();
47+
//
48+
// Thread longRunningThread = new Thread(() -> {
49+
// try {
50+
// for (int i = 0; i < 10; i++) {
51+
// System.out.println("긴 작업 스레드가 계속 실행 중...");
52+
// Thread.sleep(1000);
53+
// }
54+
// } catch (InterruptedException e) {
55+
// mainThread.interrupt();
56+
// System.out.println("긴 작업 스레드가 인터럽트 되었습니다.");
57+
// }
58+
// });
59+
//
60+
// longRunningThread.start();
61+
//
62+
// Thread interruptingThread = new Thread(() -> {
63+
// try {
64+
// System.out.println("인터럽트 스레드가 2초 후에 긴 작업 스레드를 인터럽트 합니다.");
65+
// Thread.sleep(2000);
66+
// longRunningThread.interrupt();
67+
// } catch (InterruptedException e) {
68+
// throw new RuntimeException(e);
69+
// }
70+
// });
71+
//
72+
// interruptingThread.start();
73+
//
74+
//
75+
// try {
76+
// System.out.println("메인 스레드가 긴 작업 스레드의 완료를 기다립니다.");
77+
// longRunningThread.join();
78+
// System.out.println("메인 스레드 작업 완료");
79+
//
80+
// } catch (InterruptedException e) {
81+
// System.out.println("메인 스레드가 인터럽트 되었습니다.");
82+
// throw new RuntimeException(e);
83+
// }
4584
}
4685
}

src/main/java/io/concurrency/chapter03/exam02/MultiThreadJoinExample.java

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static void main(String[] args) throws InterruptedException {
3131
thread1.join();
3232
thread2.join();
3333

34+
3435
System.out.println("메인 스레드가 계속 진행합니다");
3536

3637
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# InterruptedJoinExample
2+
3+
조인을 호출하는 스레드와, 조인의 대상이 되는 스레드와, 조인에서 인터럽트 되는 스레드를 잘 구분 지어야 한다
4+
5+
가정)<br>
6+
메인 스레드 -> 조인을 호출하는 스레드 <br>
7+
스레드 1 -> 조인의 대상이 되는 스레드. 인터럽트 되면 자신을 호출한 메서드를 인터럽트 시킴<br>
8+
스레드 2 -> 스레드 1을 인터럽트 시키는 스레드<br>
9+
10+
11+
```java
12+
13+
try{
14+
//메인 스레드 코드 블럭
15+
스레드1.start(); // 스레드 1 실행
16+
17+
스레드1.join(); //메인 스레드가 일시 중지 상태에 들어감
18+
19+
스레드2.start(); //스레드 1을 인터럽트 시키는 스레드 실행
20+
21+
//스레드 1 인터럽트 발생 -> InterruptedException 발생
22+
23+
//스레드 1의 InterruptedException에 의해 메인 스레드 인터럽트
24+
25+
// 메인 스레드의 인터럽트 발생 -> InterruptedException 발생
26+
27+
System.out.println("메인 스레드가 작업이 정상적으로 종료되었습니다");
28+
29+
}catch (InterruptedException e){
30+
System.out.println("메인 스레드가 인터럽트 되었습니다"); //해당 문구가 출력됨
31+
}
32+
```
33+
이에 따라 스레드 1은 메인 스레드를 인터럽트하는 코드를 갖고 있어야 한다
34+
35+
```java
36+
Thread mainThread = Thread.currentThread();
37+
38+
Thread longRunningThread = new Thread(() -> {
39+
try {
40+
for (int i = 0; i < 10; i++) {
41+
System.out.println("긴 스레드가 계속 실행 중...");
42+
Thread.sleep(1000);
43+
}
44+
} catch (InterruptedException e) {
45+
mainThread.interrupt();
46+
System.out.println("긴 작업 스레드가 인터럽트 되었습니다");
47+
}
48+
});
49+
```
50+
51+
### join을 호출한 스레드가 인터럽트를 받아야 wait()에서 빠져나오고 Runnable로 전환

0 commit comments

Comments
 (0)