本文主要参考:《think in java》
好,下面上货。
正常情况下,如果不做特殊的处理,在主线程中是不能够捕获到子线程中的异常的。
例如下面的情况。
-
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();
-
-
}
-
}
-
运行结果: