zoukankan      html  css  js  c++  java
  • 获取线程中抛出的异常信息

     1        ScheduledExecutorService service = Executors.newScheduledThreadPool(10);
     2         // 从现在开始delay毫秒之后,每隔一天执行一次,转换为毫秒
     3         // service.scheduleAtFixedRate(this, delay, period, TimeUnit.MILLISECONDS);
     4         /**获得线程中抛出的异常,eg Integer.parseInt("AAA");,缺少jar包等*/
     5         ScheduledFuture<?> future = service.scheduleAtFixedRate(this, delay, period, TimeUnit.MILLISECONDS);
     6         try {
     7             future.get();
     8         } catch (InterruptedException e) {
     9             Throwable cause = e.getCause();
    10             //发送邮件
    11             MailUtils.send("Write Data To HBase Faild", cause.getMessage());
    12         } catch (ExecutionException e) {
    13             Throwable cause = e.getCause();
    14             //发送邮件
    15             MailUtils.send("Write Data To HBase Faild", cause.getMessage());
    16         }

    http://stackoverflow.com/questions/2459194/no-output-from-exception

    http://stackoverflow.com/questions/18217467/scheduledexecutorservice-not-printing-the-exception-stacktrace-when-the-run-meth

    exception example:

     1 public class Playground {
     2 
     3     /**
     4      * @param args
     5      */
     6     public static void main(String[] args) {
     7         startThread();
     8     }
     9 
    10     private static void startThread() {
    11         ScheduledExecutorService timer = Executors
    12                 .newSingleThreadScheduledExecutor();
    13         Runnable r = new Runnable() {
    14             int dummyInt = 0;
    15             boolean dummyBoolean = false;
    16 
    17             @Override
    18             public void run() {
    19                 dummyInt = Integer.parseInt("AAAA");
    20 
    21                 if (dummyBoolean) {
    22                     dummyBoolean= false;
    23                 } else {
    24                     dummyBoolean= true;
    25                 }
    26 
    27             }
    28 
    29         };
    30 
    31         timer.scheduleAtFixedRate(r, 0, 100, TimeUnit.MILLISECONDS);
    32 
    33     }

    How can I get it to?

    I would expect to see this:

     1 java.lang.NumberFormatException: For input string: "AAAA"
     2     at java.lang.NumberFormatException.forInputString(Unknown Source)
     3     at java.lang.Integer.parseInt(Unknown Source)
     4     at java.lang.Integer.parseInt(Unknown Source)
     5     at Playground$1.run(Playground.java:25)
     6     at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
     7     at java.util.concurrent.FutureTask$Sync.innerRunAndReset(Unknown Source)
     8     at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
     9     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(Unknown Source)
    10     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(Unknown Source)
    11     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
    12     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    13     at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    14     at java.lang.Thread.run(Unknown Source)

    The executor probably sets its own uncaught exception handler on the thread, so the stack trace won't be printed to the console. If an exception is thrown in the Runnable you can get it from theScheduledFuture object returned by the scheduleAtFixedRate method:

    1 ScheduledFuture<?> future = timer.scheduleAtFixedRate(r, 0, 100, TimeUnit.MILLISECONDS);
    2 try {
    3     future.get();
    4 } catch (ExecutionException e) {
    5     Throwable cause = e.getCause();
    6     cause.printStackTrace();
    7 }
  • 相关阅读:
    mvc+dwz准备工作
    C# action,delegate,func的用法和区别
    mvc+dwz第二天
    mvc+dwz第一天
    H5文件上传2
    H5文件上传1
    vs2010 nuget 基础连接已经关闭:发送时发生错误
    redis分布式锁
    C# 并发队列ConcurrentQueue
    正则表达式入门
  • 原文地址:https://www.cnblogs.com/sunxucool/p/3361360.html
Copyright © 2011-2022 走看看