线程池中的异常处理
在使用 ThreadPoolExecutor 进行 submit 提交任务时,有的任务抛出了异常,但是线程池并没有进行提示,即线程池把任务中的异常给吃掉了,可以把 submit 提交改为 execute 执行,也可以对 ThreadPoolExecutor 线程池进行扩展,对提交的任务进行包装。
package com.jason.java.duoxiancheng.threadpool;
import java.util.concurrent.*;
/**
* 自定义线程池类,对 ThreadPoolExecutor 进行扩展
*
* @author Jason
*/
public class Test07 {
//子定义线程池类
private static class TraceThreadPollExecutor extends ThreadPoolExecutor {
public TraceThreadPollExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
//定义方法,对执行的任务进行包装,接收两个参数,第一个参数接收要执行的任务,第二个参数是一个 Exception 异常。
public Runnable wrap(Runnable task, Exception exception) {
return new Runnable() {
@Override
public void run() {
try {
task.run();
} catch (Exception e) {
exception.printStackTrace();
throw e;
}
}
};
}
//重写 submit 方法
@Override
public Future<?> submit(Runnable task) {
return super.submit(wrap(task, new Exception("客户跟踪异常")));
}
@Override
public void execute(Runnable command) {
super.execute(wrap(command, new Exception("客户跟踪异常")));
}
}
//定义类实现 Runnable 接口,用于计算两个数相除
private static class DivideTask implements Runnable {
private int x;
private int y;
public DivideTask(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "计算:" + x + " / " + y + " = " + (x / y));
}
}
public static void main(String[] args) {
//创建线程池
// ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 0, TimeUnit.SECONDS, new SynchronousQueue<>());
//使用自定义的线程池
ThreadPoolExecutor poolExecutor = new TraceThreadPollExecutor(0, Integer.MAX_VALUE, 0, TimeUnit.SECONDS, new SynchronousQueue<>());
//向线程池中添加计算两个数相除的任务
for (int i = 0; i < 5; i++) {
// poolExecutor.submit(new DivideTask(10, i));
poolExecutor.execute(new DivideTask(10, i));
}
}
}