Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
43 lines (34 loc) · 1.19 KB

File metadata and controls

43 lines (34 loc) · 1.19 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package ThreadTest;
/*
在没有使用线程池之前,thread/runnable接口中的run方法的异常只能在run方法里边捕获,不能冒泡到调用线程上边
接下来要说的是:线程池的异常处理器,线程池让每一个生成的线程都附着一个异常处理器
*/
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
class ExceptionHandler implements Thread.UncaughtExceptionHandler{
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.print("catch "+e);
}
}
class HandlerThreadFactory implements ThreadFactory{
@Override
public Thread newThread(Runnable r) {
Thread thread=new Thread(r);
thread.setUncaughtExceptionHandler(new ExceptionHandler());
return thread;
}
}
class ExceptionThread implements Runnable{
@Override
public void run() {
throw new RuntimeException();
}
}
public class ThreadException {
public static void main(String[] args) {
ExecutorService service=Executors.newCachedThreadPool(new HandlerThreadFactory());
service.execute(new ExceptionThread());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.