Skip to content

Commit 4d22686

Browse files
committed
UncaughtExceptionHandler
1 parent e5b7ce9 commit 4d22686

File tree

5 files changed

+117
-12
lines changed

5 files changed

+117
-12
lines changed

src/main/java/io/concurrency/chapter03/exam05/ThreadPriorityAPIExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
public class ThreadPriorityAPIExample {
44
public static void main(String[] args) {
55

6+
67
Thread thread = new Thread();
78
System.out.println("기본우선순위: " + thread.getPriority());
89
thread.start();
910

10-
1111
Thread minThread = new Thread(() -> {
1212
System.out.println("최소 우선순위 :" + Thread.currentThread().getPriority());
1313
});

src/main/java/io/concurrency/chapter03/exam05/ThreadPriorityExample.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ public static void main(String[] args) throws InterruptedException {
88
CountingThread normThread = new CountingThread("우선 순위가 기본인 스레드", Thread.NORM_PRIORITY);
99
CountingThread minThread = new CountingThread("우선 순위가 낮은 스레드", Thread.MIN_PRIORITY);
1010

11-
maxThread.start();
1211
normThread.start();
12+
maxThread.start();
1313
minThread.start();
1414

15-
maxThread.join();
16-
normThread.join();
17-
minThread.join();
15+
normThread.join();//메인 스레드가 normThread가 끝날 때까지 대기
16+
minThread.join();//메인 스레드가 minThread가 끝날 때까지 대기
17+
maxThread.join(); //메인 스레드가 maxThread가 끝날 때까지 대기
1818

1919
System.out.println("작업 완료");
2020

src/main/java/io/concurrency/chapter04/exam01/DefaultExceptionHandlerExample.java

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class DefaultExceptionHandlerExample {
88
public static void main(String[] args) {
99

1010
// 모든 스레드의 예외에 대한 기본 핸들러 설정
11+
//이 핸들러를 호출하는 것은 메인 스레드! 따라서 메인(외부)에서 여러 스레드의 전역적인 예외 캐치 가능하다
1112
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
1213
@Override
1314
public void uncaughtException(Thread t, Throwable e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# UncaughtException
2+
3+
run() 메서드는 스레드 예외를 캐치하지 못하기 떄문에 스레드에서 발생한 예외를 캐치하려면 UncaughtExceptionHandler를 사용해야 한다
4+
5+
### setDefaultUncaughtExceptionHandler
6+
- static 메서드로 전역적으로 모든 스레드의 예외를 캐치함
7+
- 메인 스레드에서 다른 스레드들의 예외를 캐치하는 것 가능 -> 즉 외부에서 여러 스레드들의 예외 캐치 가능!!
8+
```java
9+
// 모든 스레드의 예외에 대한 기본 핸들러 설정
10+
//이 핸들러를 호출하는 것은 메인 스레드! 따라서 메인(외부)에서 여러 스레드의 전역적인 예외 캐치 가능하다
11+
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
12+
@Override
13+
public void uncaughtException(Thread t, Throwable e) {
14+
System.out.println(t.getName() + " 에서 예외 발생 " + e);
15+
}
16+
});
17+
18+
// 예외를 발생시키는 여러 스레드
19+
Thread thread1 = new Thread(() -> {
20+
throw new RuntimeException("스레드 1 예외!");
21+
});
22+
23+
Thread thread2 = new Thread(() -> {
24+
throw new RuntimeException("스레드 2 예외!");
25+
});
26+
27+
thread1.start();
28+
thread2.start();
29+
```
30+
31+
### setUncaughtExceptionHandler
32+
- 대상 스레드에서 발생하는 UncaughtException을 처리하는 인스턴스 메서드
33+
- setDefaultUncaughtExceptionHandler보다 우선 순위 높음 -> 따라서 setDefaultUncaughtExceptionHandler를 구현했더라도, 대상 스레드에서 구현한 setUncaughtExceptionHandler이 있다면 이게 실행됨
34+
```java
35+
36+
private static final Logger LOGGER = Logger.getLogger(UncaughtExceptionHandlerExample.class.getName());
37+
38+
public static void main(String[] args) {
39+
Thread thread1 = new Thread(() -> {
40+
System.out.println("스레드 1 시작!");
41+
throw new RuntimeException("예기치 않은 예외!");
42+
});
43+
44+
Thread thread2 = new Thread(() -> {
45+
System.out.println("스레드 2 시작!");
46+
throw new RuntimeException("예기치 않은 예외!");
47+
});
48+
49+
thread1.setUncaughtExceptionHandler((t, e) -> {
50+
LOGGER.log(Level.SEVERE, t.getName() + "에서 예외 발생 : "+e);
51+
sendNotificationToAdmin(e);
52+
});
53+
54+
thread2.setUncaughtExceptionHandler((t, e) -> {
55+
LOGGER.log(Level.SEVERE, t.getName()+"에서 예외 발생했긔!!! : "+e);
56+
sendNotificationToAdmin(e);
57+
});
58+
59+
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
60+
@Override
61+
public void uncaughtException(Thread t, Throwable e) {
62+
System.out.println(t.getName()+"에서 발생한 예외 전역적으로 캐치햇긔!! : "+e);
63+
}
64+
});
65+
66+
thread1.start();
67+
thread2.start();
68+
}
69+
70+
// 알림 서비스를 호출하는 메서드
71+
private static void sendNotificationToAdmin(Throwable e) {
72+
System.out.println("관리자에게 알림: " + e.getMessage());
73+
}
74+
75+
```
76+
- thread1 예외 발생 시 -> SEVERE: Thread-0에서 예외 발생 : java.lang.RuntimeException: 예기치 않은 예외!
77+
- thread2 예외 발생 시 -> SEVERE: Thread-1에서 예외 발생했긔!!! : java.lang.RuntimeException: 예기치 않은 예외!

src/main/java/io/concurrency/chapter04/exam01/UncaughtExceptionHandlerExample.java

+34-7
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,49 @@ public class UncaughtExceptionHandlerExample {
77
private static final Logger LOGGER = Logger.getLogger(UncaughtExceptionHandlerExample.class.getName());
88

99
public static void main(String[] args) {
10-
Thread thread = new Thread(() -> {
11-
System.out.println("스레드 시작!");
10+
11+
12+
Thread thread1 = new Thread(() -> {
13+
System.out.println("스레드 1 시작!");
1214

1315
// 예기치 않은 예외 발생
1416
throw new RuntimeException("예기치 않은 예외!");
1517
});
1618

17-
// 스레드의 UncaughtExceptionHandler 설정
18-
thread.setUncaughtExceptionHandler((t, e) -> {
19-
LOGGER.log(Level.SEVERE, t.getName() + " 에서 예외가 발생했습니다.", e);
19+
Thread thread2 = new Thread(() -> {
20+
System.out.println("스레드 2 시작!");
21+
22+
// 예기치 않은 예외 발생
23+
throw new RuntimeException("예기치 않은 예외!");
24+
});
2025

21-
// 오류가 발생한 경우 알림 서비스 호출 (예: 이메일 또는 Slack 알림)
26+
thread1.setUncaughtExceptionHandler((t, e) -> {
27+
LOGGER.log(Level.SEVERE, t.getName() + "에서 예외 발생 : "+e);
2228
sendNotificationToAdmin(e);
2329
});
2430

25-
thread.start();
31+
thread2.setUncaughtExceptionHandler((t, e) -> {
32+
LOGGER.log(Level.SEVERE, t.getName()+"에서 예외 발생했긔!!! : "+e);
33+
sendNotificationToAdmin(e);
34+
});
35+
36+
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
37+
@Override
38+
public void uncaughtException(Thread t, Throwable e) {
39+
System.out.println(t.getName()+"에서 발생한 예외 전역적으로 캐치햇긔!! : "+e);
40+
}
41+
});
42+
43+
// 스레드의 UncaughtExceptionHandler 설정
44+
// thread.setUncaughtExceptionHandler((t, e) -> {
45+
// LOGGER.log(Level.SEVERE, t.getName() + " 에서 예외가 발생했습니다.", e);
46+
//
47+
// // 오류가 발생한 경우 알림 서비스 호출 (예: 이메일 또는 Slack 알림)
48+
// sendNotificationToAdmin(e);
49+
// });
50+
51+
thread1.start();
52+
thread2.start();
2653
}
2754

2855
// 알림 서비스를 호출하는 메서드

0 commit comments

Comments
 (0)