java主线程捕获子线程中的异常

本文主要参考:《think in java》

好,下面上货。

正常情况下,如果不做特殊的处理,在主线程中是不能够捕获到子线程中的异常的。
例如下面的情况。
package com.xueyou.demo.theadexceptiondemo;
public class ThreadExceptionRunner implements Runnable{
@Override
public void run() {
throw new RuntimeException("error !!!!");
}
}
使用线程执行上面的任务
package com.xueyou.demo.theadexceptiondemo;
import com.sun.glass.ui.TouchInputSupport;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
public class ThreadExceptionDemo {
public static void main(String[] args) {
try {
Thread thread = new Thread(new ThreadExceptionRunner());
thread.start();
} catch (Exception e) {
System.out.println("========");
e.printStackTrace();
} finally {
}
System.out.println(123);
}
}
执行结果如下:


如果想要在主线程中捕获子线程的异常,我们需要使用ExecutorService,同时做一些修改。
如下:

package com.xueyou.demo.theadexceptiondemo;
import com.sun.glass.ui.TouchInputSupport;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
public class ThreadExceptionDemo {
public static void main(String[] args) {
try {
Thread thread = new Thread(new ThreadExceptionRunner());
thread.start();
} catch (Exception e) {
System.out.println("========");
e.printStackTrace();
} finally {
}
System.out.println(123);
ExecutorService exec = Executors.newCachedThreadPool(new HandleThreadFactory());
exec.execute(new ThreadExceptionRunner());
exec.shutdown();
}
}
class MyUncaughtExceptionHandle implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("caught " + e);
}
}
class HandleThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable r) {
System.out.println("create thread t");
Thread t = new Thread(r);
System.out.println("set uncaughtException for t");
t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandle());
return t;
}
}
这样就能够捕获到异常了,运行结果如下:



上面的方式是设置每一个线程执行时候的异常处理。如果每一个线程的异常处理相同,我们可以用如下的方式进行处理,使用Thread的静态方法。
Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandle());

整体代码如下:

package com.xueyou.demo.theadexceptiondemo;
import com.sun.glass.ui.TouchInputSupport;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
/**
* Created by wuxueyou on 2018/6/24.
*/
public class ThreadExceptionDemo {
public static void main(String[] args) {
try {
Thread thread = new Thread(new ThreadExceptionRunner());
thread.start();
} catch (Exception e) {
System.out.println("========");
e.printStackTrace();
} finally {
}
System.out.println(123);
Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandle());
// ExecutorService exec = Executors.newCachedThreadPool(new HandleThreadFactory());
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new ThreadExceptionRunner());
exec.shutdown();
}
}
运行结果:

转载于:https://www.cnblogs.com/jpfss/p/10272726.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值