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

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

好,下面上货。python

正常状况下,若是不作特殊的处理,在主线程中是不可以捕获到子线程中的异常的。
例以下面的状况。
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();

    }
}
运行结果: