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
  • 相关阅读:
    【转】PostgreSQL与MySQL比较
    HIVE出现Read past end of RLE integer from compressed stream Stream for column 1 kind LENGTH position: 359 length: 359 range: 0错误
    HSDF查看各级目录的大小
    windows7搜索python java go php等其他文件内容
    Tomcat配置https后,并发较大时,频繁超时情况。
    Tomcat7配置Https
    部分手机浏览器存在将ajax请求当成广告过滤的情况,及解决方案
    百度广告联盟api probuf协议对接
    SQL查询时,根据日期范围查询周
    执行Hive出现Error running child : java.lang.OutOfMemoryError: Java heap space错误
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3751875.html
Copyright © 2011-2022 走看看