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 }
  • 相关阅读:
    mapper.xml接收多个参数
    java循环调用多个timer定时器
    java执行cmd命令的两种方法
    java解压.ZIP .TAR等常用格式文件
    HDU2157 (水题)状态转移
    Codeforces Round #628 (Div. 2)C. Ehab and Path-etic MEXs(构造+树)
    POJ3735——mat乘法优化
    HDU5667——费马小定理
    UCF Local Programming Contest 2012(Practice) D. The Clock Algorithm
    嵌套递推——矩阵快速幂
  • 原文地址:https://www.cnblogs.com/sunxucool/p/3361360.html
Copyright © 2011-2022 走看看