2026/2/16 7:31:15
网站建设
项目流程
网站建设万首先金手指12,做淘宝联盟网站用数据库吗,运营一个企业网站的推广方案,做企业网站注意些啥在实际开发中#xff0c;我们常常会用到线程池#xff0c;但任务一旦提交到线程池之后#xff0c;如果发生异常之后#xff0c;怎么处理#xff1f; 怎么获取到异常信息#xff1f;在了解这个问题之前#xff0c;可以先看一下 线程池的源码解析#xff0c;从源码中我们…在实际开发中我们常常会用到线程池但任务一旦提交到线程池之后如果发生异常之后怎么处理 怎么获取到异常信息在了解这个问题之前可以先看一下 线程池的源码解析从源码中我们知道了线程池的提交方式submit和execute的区别接下来分别使用他们执行带有异常的任务看结果是怎么样的我们先用伪代码模拟一下线程池抛异常的场景public class ThreadPoolException { public static void main(String[] args) { //创建一个线程池 ExecutorService executorService Executors.newFixedThreadPool(1); //当线程池抛出异常后 submit无提示其他线程继续执行 executorService.submit(new task()); //当线程池抛出异常后 execute抛出异常其他线程继续执行新任务 executorService.execute(new task()); } } //任务类 class task implements Runnable{ Override public void run() { System.out.println(进入了task方法); int i1/0; } }运行结果可以看到submit不打印异常信息而execute则会打印异常信息submit的方式不打印异常信息显然在生产中是不可行的因为我们无法保证线程中的任务永不异常而如果使用submit的方式出现了异常直接如上写法我们将无法获取到异常信息做出对应的判断和处理所以下一步需要知道如何获取线程池抛出的异常推荐Java工程师技术指南https://github.com/chenjiabing666/JavaFamilysubmit()想要获取异常信息就必须使用get()方法//当线程池抛出异常后 submit无提示其他线程继续执行 Future? submit executorService.submit(new task()); submit.get();submit打印异常信息如下方案一使用try -catchpublic class ThreadPoolException { public static void main(String[] args) { //创建一个线程池 ExecutorService executorService Executors.newFixedThreadPool(1); //当线程池抛出异常后 submit无提示其他线程继续执行 executorService.submit(new task()); //当线程池抛出异常后 execute抛出异常其他线程继续执行新任务 executorService.execute(new task()); } } // 任务类 class task implements Runnable { Override public void run() { try { System.out.println(进入了task方法); int i 1 / 0; } catch (Exception e) { System.out.println(使用了try -catch 捕获异常 e); } } }打印结果可以看到 submit 和 execute都清晰易懂的捕获到了异常可以知道我们的任务出现了问题而不是消失的无影无踪。关注公众号码猿技术专栏回复关键词1111 获取阿里内部Java性能调优手册方案二使用Thread.setDefaultUncaughtExceptionHandler方法捕获异常方案一中每一个任务都要加一个try-catch实在是太麻烦了而且代码也不好看那么这样想的话可以用Thread.setDefaultUncaughtExceptionHandler方法捕获异常UncaughtExceptionHandler是Thread类一个内部类也是一个函数式接口。推荐Java工程师技术指南https://github.com/chenjiabing666/JavaFamily内部的uncaughtException是一个处理线程内发生的异常的方法参数为线程对象t和异常对象e。应用在线程池中如下所示重写它的线程工厂方法在线程工厂创建线程的时候都赋予UncaughtExceptionHandler处理器对象。public class ThreadPoolException { public static void main(String[] args) throws InterruptedException { //1.实现一个自己的线程池工厂 ThreadFactory factory (Runnable r) - { //创建一个线程 Thread t new Thread(r); //给创建的线程设置UncaughtExceptionHandler对象 里面实现异常的默认逻辑 t.setDefaultUncaughtExceptionHandler((Thread thread1, Throwable e) - { System.out.println(线程工厂设置的exceptionHandler e.getMessage()); }); return t; }; //2.创建一个自己定义的线程池使用自己定义的线程工厂 ExecutorService executorService new ThreadPoolExecutor( 1, 1, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(10), factory); // submit无提示 executorService.submit(new task()); Thread.sleep(1000); System.out.println(为检验打印结果1秒后执行execute方法); // execute 方法被线程工厂factory 的UncaughtExceptionHandler捕捉到异常 executorService.execute(new task()); } } class task implements Runnable { Override public void run() { System.out.println(进入了task方法); int i 1 / 0; } }打印结果如下根据打印结果我们看到execute方法被线程工厂factory中设置的UncaughtExceptionHandler捕捉到异常而submit方法却没有任何反应说明UncaughtExceptionHandler在submit中并没有被调用。这是为什么呢在日常使用中我们知道execute和submit最大的区别就是execute没有返回值submit有返回值。submit返回的是一个future 可以通过这个future取到线程执行的结果或者异常信息。Future? submit executorService.submit(new task()); //打印异常结果 System.out.println(submit.get());从结果看出submit并不是丢失了异常使用future.get还是有异常打印的那为什么线程工厂factory 的UncaughtExceptionHandler没有打印异常呢猜测是submit方法内部已经捕获了异常 只是没有打印出来也因为异常已经被捕获因此jvm也就不会去调用Thread的UncaughtExceptionHandler去处理异常。接下来验证猜想首先看一下submit和execute的源码execute方法的源码在这博客中写的很详细点击查看execute源码在此就不再啰嗦了https://blog.csdn.net/qq_45076180/article/details/108316340submit源码在底层还是调用的execute方法只不过多一层Future封装并返回了这个Future这也解释了为什么submit会有返回值//submit()方法 public T FutureT submit(CallableT task) { if (task null) throw new NullPointerException(); //execute内部执行这个对象内部的逻辑然后将结果或者异常 set到这个ftask里面 RunnableFutureT ftask newTaskFor(task); // 执行execute方法 execute(ftask); //返回这个ftask return ftask; }可以看到submit也是调用的execute在execute方法中我们的任务被提交到了addWorker(command, true)然后为每一个任务创建一个Worker去处理这个线程这个Worker也是一个线程执行任务时调用的就是Worker的run方法run方法内部又调用了runworker方法如下所示public void run() { runWorker(this); } final void runWorker(Worker w) { Thread wt Thread.currentThread(); Runnable task w.firstTask; w.firstTask null; w.unlock(); // allow interrupts boolean completedAbruptly true; try { //这里就是线程可以重用的原因循环条件判断不断从队列中取任务 //还有一个问题就是非核心线程的超时删除是怎么解决的 //主要就是getTask方法()见下文③ while (task ! null || (task getTask()) ! null) { w.lock(); if ((runStateAtLeast(ctl.get(), STOP) || (Thread.interrupted() runStateAtLeast(ctl.get(), STOP))) !wt.isInterrupted()) wt.interrupt(); try { beforeExecute(wt, task); Throwable thrown null; try { //执行线程 task.run(); //异常处理 } catch (RuntimeException x) { thrown x; throw x; } catch (Error x) { thrown x; throw x; } catch (Throwable x) { thrown x; throw new Error(x); } finally { //execute的方式可以重写此方法处理异常 afterExecute(task, thrown); } } finally { task null; w.completedTasks; w.unlock(); } } //出现异常时completedAbruptly不会被修改为false completedAbruptly false; } finally { //如果如果completedAbruptly值为true则出现异常则添加新的Worker处理后边的线程 processWorkerExit(w, completedAbruptly); } }核心就在task.run();这个方法里面了 期间如果发生异常会被抛出。如果用execute提交的任务会被封装成了一个runable任务然后进去 再被封装成一个worker,最后在worker的run方法里面调用runWoker方法runWoker方法里面执行任务任务如果任务出现异常用try-catch捕获异常往外面抛我们在最外层使用try-catch捕获到了runWoker方法中抛出的异常。因此我们在execute中看到了我们的任务的异常信息。那么为什么submit没有异常信息呢 因为submit是将任务封装成了一个futureTask然后这个futureTask被封装成worker在woker的run方法里面最终调用的是futureTask的run方法 猜测里面是直接吞掉了异常并没有抛出异常因此在worker的runWorker方法里面无法捕获到异常。下面来看一下futureTask的run方法果不其然在try-catch中吞掉了异常将异常放到了setException(ex);里面public void run() { if (state ! NEW || !UNSAFE.compareAndSwapObject(this, runnerOffset, null, Thread.currentThread())) return; try { CallableV c callable; if (c ! null state NEW) { V result; boolean ran; try { result c.call(); ran true; } catch (Throwable ex) { result null; ran false; //在此方法中设置了异常信息 setException(ex); } if (ran) set(result); } //省略下文 。。。。。。 setException(ex)方法如下将异常对象赋予outcome protected void setException(Throwable t) { if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) { //将异常对象赋予outcome记住这个outcome outcome t; UNSAFE.putOrderedInt(this, stateOffset, EXCEPTIONAL); // final state finishCompletion(); } }将异常对象赋予outcome有什么用呢这个outcome是什么呢当我们使用submit返回Future对象并使用Future.get()时 会调用内部的report方法public V get() throws InterruptedException, ExecutionException { int s state; if (s COMPLETING) s awaitDone(false, 0L); //注意这个方法 return report(s); }reoport里面实际上返回的是outcome ,刚好之前的异常就set到了这个outcome里面private V report(int s) throws ExecutionException { //设置outcome Object x outcome; if (s NORMAL) //返回outcome return (V)x; if (s CANCELLED) throw new CancellationException(); throw new ExecutionException((Throwable)x); }因此在用submit提交的时候runable对象被封装成了future future 里面的 run方法在处理异常时try-catch了所有的异常通过setException(ex);方法设置到了变量outcome里面 可以通过future.get获取到outcome。所以在submit提交的时候里面发生了异常 是不会有任何抛出信息的。而通过future.get可以获取到submit抛出的异常在submit里面除了从返回结果里面取到异常之外, 没有其他方法。因此在不需要返回结果的情况下最好用execute 这样就算没有写try-catch疏漏了异常捕捉也不至于丢掉异常信息。方案三重写afterExecute进行异常处理通过上述源码分析在excute的方法里面可以通过重写afterExecute进行异常处理但是注意 这个也只适用于excute提交(submit的方式比较麻烦下面说)因为submit的task.run里面把异常吞了根本不会跑出来异常因此也不会有异常进入到afterExecute里面。在runWorker里面调用task.run之后会调用线程池的afterExecute(task, thrown)方法final void runWorker(Worker w) { //当前线程 Thread wt Thread.currentThread(); //我们的提交的任务 Runnable task w.firstTask; w.firstTask null; w.unlock(); // allow interrupts boolean completedAbruptly true; try { while (task ! null || (task getTask()) ! null) { w.lock(); if ((runStateAtLeast(ctl.get(), STOP) || (Thread.interrupted() runStateAtLeast(ctl.get(), STOP))) !wt.isInterrupted()) wt.interrupt(); try { beforeExecute(wt, task); Throwable thrown null; try { //直接就调用了task的run方法 task.run(); //如果是futuretask的run,里面是吞掉了异常不会有异常抛出 // 因此Throwable thrown null; 也不会进入到catch里面 } catch (RuntimeException x) { thrown x; throw x; } catch (Error x) { thrown x; throw x; } catch (Throwable x) { thrown x; throw new Error(x); } finally { //调用线程池的afterExecute方法 传入了task和异常 afterExecute(task, thrown); } } finally { task null; w.completedTasks; w.unlock(); } } completedAbruptly false; } finally { processWorkerExit(w, completedAbruptly); } }重写afterExecute处理execute提交的异常public class ThreadPoolException3 { public static void main(String[] args) throws InterruptedException, ExecutionException { //1.创建一个自己定义的线程池 ExecutorService executorService new ThreadPoolExecutor( 2, 3, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(10) ) { //重写afterExecute方法 Override protected void afterExecute(Runnable r, Throwable t) { System.out.println(afterExecute里面获取到异常信息处理异常 t.getMessage()); } }; //当线程池抛出异常后 execute executorService.execute(new task()); } } class task3 implements Runnable { Override public void run() { System.out.println(进入了task方法); int i 1 / 0; } }执行结果我们可以在afterExecute方法内部对异常进行处理如果要用这个afterExecute处理submit提交的异常 要额外处理。判断Throwable是否是FutureTask如果是代表是submit提交的异常代码如下public class ThreadPoolException3 { public static void main(String[] args) throws InterruptedException, ExecutionException { //1.创建一个自己定义的线程池 ExecutorService executorService new ThreadPoolExecutor( 2, 3, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(10) ) { //重写afterExecute方法 Override protected void afterExecute(Runnable r, Throwable t) { //这个是excute提交的时候 if (t ! null) { System.out.println(afterExecute里面获取到excute提交的异常信息处理异常 t.getMessage()); } //如果r的实际类型是FutureTask 那么是submit提交的所以可以在里面get到异常 if (r instanceof FutureTask) { try { Future? future (Future?) r; //get获取异常 future.get(); } catch (Exception e) { System.out.println(afterExecute里面获取到submit提交的异常信息处理异常 e); } } } }; //当线程池抛出异常后 execute executorService.execute(new task()); //当线程池抛出异常后 submit executorService.submit(new task()); } } class task3 implements Runnable { Override public void run() { System.out.println(进入了task方法); int i 1 / 0; } }处理结果如下可以看到使用重写afterExecute这种方式既可以处理execute抛出的异常也可以处理submit抛出的异常