欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于Java子线程中的异常处理方法(通用)

程序员文章站 2024-03-02 20:45:10
在普通的单线程程序中,捕获异常只需要通过try ... catch ... finally ...代码块就可以了。那么,在并发情况下,比如在父线程中启动了子线程,如何在父线...

在普通的单线程程序中,捕获异常只需要通过try ... catch ... finally ...代码块就可以了。那么,在并发情况下,比如在父线程中启动了子线程,如何在父线程中捕获来自子线程的异常,从而进行相应的处理呢?

常见错误

也许有人会觉得,很简单嘛,直接在父线程启动子线程的地方try ... catch一把就可以了,其实这是不对的。

原因分析

让我们回忆一下runnable接口的run方法的完整签名,因为没有标识throws语句,所以方法是不会抛出checked异常的。至于runtimeexception这样的unchecked异常,由于新线程由jvm进行调度执行,如果发生了异常,也不会通知到父线程。

public abstract void run()

解决办法

那么,如何在父线程中捕获来自子线程的异常呢?楼主想到了3种常用方法,分享给大家。

方法一:子线程中try... catch...

最简单有效的办法,就是在子线程的方法中,把可能发生异常的地方,用try ... catch ... 语句包起来。子线程代码:

public class childthread implements runnable {
 public void run() {
  dosomething1();
  try {
   // 可能发生异常的方法
   exceptionmethod();
  } catch (exception e) {
   // 处理异常
   system.out.println(string.format("handle exception in child thread. %s", e));
  }
  dosomething2();
 }
}

方法二:为线程设置异常处理器uncaughtexceptionhandler

为线程设置异常处理器。具体做法可以是以下几种:

(1)thread.setuncaughtexceptionhandler设置当前线程的异常处理器

(2)thread.setdefaultuncaughtexceptionhandler为整个程序设置默认的异常处理器如果当前线程有异常处理器(默认没有),则优先使用该uncaughtexceptionhandler类;否则,如果当前线程所属的线程组有异常处理器,则使用线程组的exceptionhandler;否则,使用全局默认的defaultuncaughtexceptionhandler;如果都没有的话,子线程就会退出。

注意:子线程中发生了异常,如果没有任何类来接手处理的话,是会直接退出的,而不会留下打印任何日志。所以,如果什么都不做的话,是会出现子线程任务既没执行,也没有任何日志提示的“诡异”现象的。

设置当前线程的异常处理器:

public class childthread implements runnable { 
 private static childthreadexceptionhandler exceptionhandler;

 static {
  exceptionhandler = new childthreadexceptionhandler();
 }

 public void run() {
  thread.currentthread().setuncaughtexceptionhandler(exceptionhandler);
  system.out.println("do something 1");
  exceptionmethod();
  system.out.println("do something 2");
 }

 public static class childthreadexceptionhandler implements thread.uncaughtexceptionhandler {
  public void uncaughtexception(thread t, throwable e) {
   system.out.println(string.format("handle exception in child thread. %s", e));
  }
 }
}

或者,设置所有线程的默认异常处理器

public class childthread implements runnable {
 private static childthreadexceptionhandler exceptionhandler;

 static {
  exceptionhandler = new childthreadexceptionhandler();
  thread.setdefaultuncaughtexceptionhandler(exceptionhandler);
 }

 public void run() {
  system.out.println("do something 1");
  exceptionmethod();
  system.out.println("do something 2");
 }

 private void exceptionmethod() {
  throw new runtimeexception("childthread exception");
 }

 public static class childthreadexceptionhandler implements thread.uncaughtexceptionhandler {
  public void uncaughtexception(thread t, throwable e) {
   system.out.println(string.format("handle exception in child thread. %s", e));
  }
 }
}

命令行输出:do something 1

handle exception in child thread. java.lang.runtimeexception: childthread exception

方法三,通过future的get方法捕获异常

使用线程池提交一个能获取到返回信息的方法,也就是executorservice.submit(callable)在submit之后可以获得一个线程执行结果的future对象,而如果子线程中发生了异常,通过future.get()获取返回值时,可以捕获到executionexception异常,从而知道子线程中发生了异常。

子线程代码:

public class childthread implements callable {
 public object call() throws exception {
  system.out.println("do something 1");
  exceptionmethod();
  system.out.println("do something 2");
  return null;
 }

 private void exceptionmethod() {
  throw new runtimeexception("childthread1 exception");
 }
}

父线程代码:

public class main {
 public static void main(string[] args) {
  executorservice executorservice = executors.newfixedthreadpool(8);
  future future = executorservice.submit(new childthread());
  try {
   future.get();
  } catch (interruptedexception e) {
   system.out.println(string.format("handle exception in child thread. %s", e));
  } catch (executionexception e) {
   system.out.println(string.format("handle exception in child thread. %s", e));
  } finally {
   if (executorservice != null) {
    executorservice.shutdown();
   }
  }
 }
}

命令行输出:do something 1

handle exception in child thread. java.util.concurrent.executionexception: java.lang.runtimeexception: childthread1 exception

总结

上面就是3种常用的java子线程异常处理方法。其实楼主还想到了另外几个特定场景下的解决办法,改天再分析,谢谢大家支持~

这篇基于java子线程中的异常处理方法(通用)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。