Java多线程之捕获异常

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 Thread
created Thread[Thread-0,5,main] ID:9
eh=Thread.Exection.MyUncaughtExceptionHandler@70dea4e
run() by Thread[Thread-0,5,main]
eh=Thread.Exection.MyUncaughtExceptionHandler@70dea4e
Thread.Exection.HandlerThreadFactory@4e25154f creating new Thread
created Thread[Thread-1,5,main] ID:11
eh=Thread.Exection.MyUncaughtExceptionHandler@193fa958
caught 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值