Java多线程之捕获异常

来源:互联网 发布:淘宝产品分类 编辑:程序博客网 时间:2024/06/06 07:12
1.主线程不能捕获到子线程的异常
package Thread.Exection;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ExeceptionThread implements Runnable {    @Override    public void run() {        throw new RuntimeException();    }    public static void main(String[] args) {        try {            ExecutorService exec = Executors.newCachedThreadPool();            exec.execute(new ExeceptionThread());        } catch (Exception e) {            System.out.println("exeception has handled");        }    }}

输出

Exception in thread "pool-1-thread-1" java.lang.RuntimeException    at Thread.Exection.ExeceptionThread.run(ExeceptionThread.java:10)    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)    at java.lang.Thread.run(Unknown Source)
2.通过设置HandlerThreadFactory捕获异常

package Thread.Exection;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.ThreadFactory;class ExceptionThread2 implements Runnable {    public void run() {        Thread t = Thread.currentThread();        System.out.println("run() by " + t);        System.out.println("eh=" + t.getUncaughtExceptionHandler());        throw new RuntimeException();    }}class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {    @Override    public void uncaughtException(Thread t, Throwable e) {        System.out.println("caught " + e);    }}class HandlerThreadFactory implements ThreadFactory {    @Override    public Thread newThread(Runnable r) {        System.out.println(this + " creating new Thread");        Thread t = new Thread(r);        System.out.println("created " + t + " ID:" + t.getId());        t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());        System.out.println("eh=" + t.getUncaughtExceptionHandler());        return t;    }}public class CaptureUncaughtException {    public static void main(String[] args) {        ExecutorService exec = Executors                .newCachedThreadPool(new HandlerThreadFactory());        exec.execute(new ExceptionThread2());    }}
输出

Thread.Exection.HandlerThreadFactory@4e25154f creating new Threadcreated Thread[Thread-0,5,main] ID:9eh=Thread.Exection.MyUncaughtExceptionHandler@70dea4erun() by Thread[Thread-0,5,main]eh=Thread.Exection.MyUncaughtExceptionHandler@70dea4eThread.Exection.HandlerThreadFactory@4e25154f creating new Threadcreated Thread[Thread-1,5,main] ID:11eh=Thread.Exection.MyUncaughtExceptionHandler@193fa958caught java.lang.RuntimeException

3.通过设置默认异常捕获类捕获异常

package Thread.Exection;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class SettingDefaultHandler {    public static void main(String[] args) {        Thread.setDefaultUncaughtExceptionHandler(                new MyUncaughtExceptionHandler());                ExecutorService exec=Executors.newCachedThreadPool();        exec.execute(new ExceptionThread2());    }}

输出

run() by Thread[pool-1-thread-1,5,main]eh=java.lang.ThreadGroup[name=main,maxpri=10]caught java.lang.RuntimeException




0 0
原创粉丝点击