异常处理 ScheduledExecutorService
Exception Handling ScheduledExecutorService
我以 1 min
的固定间隔使用 ScheduledExecutorService
至 运行 个线程。
一个实例 ScheduledExecutorService
运行 一个线程,另一个实例 运行 另一个线程。
示例:
ses1.scheduleAtFixRate(..) // for thread 1
ses2.scheduleAtFixRate(..) // for thread 2
我遇到了一些导致进一步执行停止的异常。我想捕获系统关闭我的应用程序的异常。
我应该使用第三个线程 来处理异常,该线程监视两个 futures 并处理异常,还是有其他更好的方法?会不会影响其他线程。
感谢任何帮助!
I was encountering some exceptions by which the further execution
stops.
根据规范,这是 ScheduledExecutorService.scheduleAtFixRate()
的预期行为:
If any execution of the task encounters an exception, subsequent
executions are suppressed.
关于您的需求:
I want to catch the exception for a systematic shutdown of my
application.
Should I handle the exception using a third thread that monitors both
futures and handles the Exception or is there any other better way?
用 ScheduledFuture.get()
处理未来 return 看起来是正确的。
根据ScheduledFuture.scheduleAtFixedRate()
规范:
Otherwise, the task will only terminate via cancellation or
termination of the executor.
因此您甚至不需要创建新的预定未来。
只有 运行 两个并行任务(使用 ExecutorService
或两个线程也是可能的)等待每个 Future
的 get()
并且在抛出异常的情况下停止应用程序任务:
Future<?> futureA = ses1.scheduleAtFixRate(..) // for thread 1
Future<?> futureB = ses2.scheduleAtFixRate(..) // for thread 2
submitAndStopTheApplicationIfFail(futureA);
submitAndStopTheApplicationIfFail(futureB);
public void submitAndStopTheApplicationIfFail(Future<?> future){
executor.submit(() -> {
try {
future.get();
} catch (InterruptedException e) {
// stop the application
} catch (ExecutionException e) {
// stop the application
}
});
}
我以 1 min
的固定间隔使用 ScheduledExecutorService
至 运行 个线程。
一个实例 ScheduledExecutorService
运行 一个线程,另一个实例 运行 另一个线程。
示例:
ses1.scheduleAtFixRate(..) // for thread 1
ses2.scheduleAtFixRate(..) // for thread 2
我遇到了一些导致进一步执行停止的异常。我想捕获系统关闭我的应用程序的异常。
我应该使用第三个线程 来处理异常,该线程监视两个 futures 并处理异常,还是有其他更好的方法?会不会影响其他线程。
感谢任何帮助!
I was encountering some exceptions by which the further execution stops.
根据规范,这是 ScheduledExecutorService.scheduleAtFixRate()
的预期行为:
If any execution of the task encounters an exception, subsequent executions are suppressed.
关于您的需求:
I want to catch the exception for a systematic shutdown of my application.
Should I handle the exception using a third thread that monitors both futures and handles the Exception or is there any other better way?
用 ScheduledFuture.get()
处理未来 return 看起来是正确的。
根据ScheduledFuture.scheduleAtFixedRate()
规范:
Otherwise, the task will only terminate via cancellation or termination of the executor.
因此您甚至不需要创建新的预定未来。
只有 运行 两个并行任务(使用 ExecutorService
或两个线程也是可能的)等待每个 Future
的 get()
并且在抛出异常的情况下停止应用程序任务:
Future<?> futureA = ses1.scheduleAtFixRate(..) // for thread 1
Future<?> futureB = ses2.scheduleAtFixRate(..) // for thread 2
submitAndStopTheApplicationIfFail(futureA);
submitAndStopTheApplicationIfFail(futureB);
public void submitAndStopTheApplicationIfFail(Future<?> future){
executor.submit(() -> {
try {
future.get();
} catch (InterruptedException e) {
// stop the application
} catch (ExecutionException e) {
// stop the application
}
});
}