ScheduledExecutorService和未捕获的错误

rol*_*lfl 12 java exception scheduledexecutorservice

Code Review聊天中的讨论确定了ScheduledExecutorService的以下行为:

计划运行的任务失败并出现"严重"问题,但没有问题的报告,异常或日志.在其他情况下,应用程序通常会以错误终止.但是,在ScheduledExecutorService的上下文中,根本没有异常/错误"处理".

首先,要制造一个问题.以下类具有保证失败的静态初始化程序:

public class InitializerFault {

    private static final int value = Integer.parseInt("fubar");

    @Override
    public String toString() {
        return "" + value;
    }

}
Run Code Online (Sandbox Code Playgroud)

运行时:

public static void main(String[] args) {
    System.out.println(new InitializerFault());
}
Run Code Online (Sandbox Code Playgroud)

它产生(这正是我所期望的):

Exception in thread "main" java.lang.ExceptionInInitializerError
    at SimpleHandler.main(SimpleHandler.java:5)
Caused by: java.lang.NumberFormatException: For input string: "fubar"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at InitializerFault.<clinit>(InitializerFault.java:4)
    ... 1 more
Run Code Online (Sandbox Code Playgroud)

但是,当运行时:

private static final Thread buildThread(Runnable r) {
    Thread t = new Thread(r, "TestThread");
    t.setDaemon(true);
    System.out.println("Built thread " + t);
    return t;
}
public static void main(String[] args) throws InterruptedException {
    // use a thread factory to create daemon threads ... can be non-daemon as well.
    ScheduledExecutorService ses = Executors.newScheduledThreadPool(
           2, (r) -> buildThread(r));
    ses.scheduleAtFixedRate(
           () -> {System.out.println(new InitializerFault());},
           500, 1000, TimeUnit.MILLISECONDS);
    Thread.sleep(3000);
    System.out.println("Exiting");
}
Run Code Online (Sandbox Code Playgroud)

它产生的只是:

Built thread Thread[TestThread,5,main]
Exiting
Run Code Online (Sandbox Code Playgroud)

没有提到任何错误,没有错误,没有转储,没有.这个ExceptionInInitializerError导致了一个复杂的实际调试过程,其中问题很难被隔离.

两个问题:

  1. 这是预期的Java行为,执行者中的错误被"忽略"了吗?
  2. 什么是处理这种情况的正确方法?

Evg*_*eev 4

ScheduledExecutorService.scheduleAtFixedRate返回一个ScheduledFuture. 如果我们调用,ScheduledFuture.get()线程将阻塞并等待周期性任务完成,这可能会在任务取消或任务抛出异常时发生。在后一种情况下, get() 将抛出java.util.concurrent.ExecutionException带有包装的原始异常

  • 是的,我现在已经尝试过了,(当然)你是对的,确实如此。虽然你的答案是正确的,但这是一个丑陋的解决方案......真正的任务本身使用 try/catch 块进行常规异常处理,但不会捕获错误...此外,在主线程(或非线程)上“等待”执行线程)不太实用。如果我想在那里阻塞,我就不会使用延迟。这不是你的错,但你建议的解决方案是“丑陋的”。 (4认同)