zoukankan      html  css  js  c++  java
  • Java子线程中的异常处理(通用)

    参考 连接

      https://www.cnblogs.com/jpfss/p/10272066.html

    核心代码:

    子线程代码:
    public class ChildThread implements Callable<String> {
        public String call() throws Exception {
            System.out.println("do something 1");
            exceptionMethod();
            System.out.println("do something 2");
            return "test result";
        }
    
        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();
                }
            }
        }
    }


  • 相关阅读:
    其他
    Win10
    Win10
    面向对象与设计模式
    Git
    Java
    Git
    Git
    Git
    一、I/O操作(File文件对象)
  • 原文地址:https://www.cnblogs.com/kpwong/p/15342046.html
Copyright © 2011-2022 走看看