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导致了一个复杂的实际调试过程,其中问题很难被隔离.
两个问题:
ScheduledExecutorService.scheduleAtFixedRate
返回一个ScheduledFuture
. 如果我们调用,ScheduledFuture.get()
线程将阻塞并等待周期性任务完成,这可能会在任务取消或任务抛出异常时发生。在后一种情况下, get() 将抛出java.util.concurrent.ExecutionException
带有包装的原始异常
归档时间: |
|
查看次数: |
416 次 |
最近记录: |