zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    实战DeviceIoControl 之五:列举已安装的存储设备
    在NT中直接访问物理内存
    实战DeviceIoControl 之三:制作磁盘镜像文件
    实战DeviceIoControl 之六:访问物理端口
    程序员的十层楼(转)
    Vista + VS2005 源代码绑定的问题
    敬告天下IT业主
    手动卸载windows服务
    古墓丽影9的截屏
    白领饮食十大“夺命”恶习(转)
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3751875.html
Copyright © 2011-2022 走看看