public class ThreadPoolException { public static void main(String[] args) { //创建一个线程池 ExecutorService executorService= Executors.newFixedThreadPool(1); //当线程池抛出异常后 submit无提示,其他线程继续执行 executorService.submit(new task()); //当线程池抛出异常后 execute抛出异常,其他线程继续执行新任务 executorService.execute(new task()); } } // 堆代码 duidaima.com //任务类 class task implements Runnable{ @Override public void run() { System.out.println("进入了task方法!!!"); int i=1/0; } }运行结果:
//当线程池抛出异常后 submit无提示,其他线程继续执行 Future<?> submit = executorService.submit(new task()); submit.get();submit打印异常信息如下:
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() { try { System.out.println("进入了task方法!!!"); int i = 1 / 0; } catch (Exception e) { System.out.println("使用了try -catch 捕获异常" + e); } } }打印结果:
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; } }打印结果如下:
Future<?> submit = executorService.submit(new task()); //打印异常结果 System.out.println(submit.get());
//submit()方法 public <T> Future<T> submit(Callable<T> task) { if (task == null) throw new NullPointerException(); //execute内部执行这个对象内部的逻辑,然后将结果或者异常 set到这个ftask里面 RunnableFuture<T> 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; thrownew 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中看到了我们的任务的异常信息。
public void run() { if (state != NEW || !UNSAFE.compareAndSwapObject(this, runnerOffset, null, Thread.currentThread())) return; try { Callable<V> 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。
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; thrownew 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方法内部对异常进行处理
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; } }处理结果如下: