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();
                }
            }
        }
    }


  • 相关阅读:
    指针
    显示和隐式转换
    C++虚函数
    字符串输出
    BP神经网络
    超像素分割
    函数putText()在图片上写文字
    compare
    十五、cookies和session的使用
    爬取腾讯社招职位信息
  • 原文地址:https://www.cnblogs.com/kpwong/p/15342046.html
Copyright © 2011-2022 走看看