这是我的主要方法:
public static void main(String... args) throws ClassNotFoundException {
String[] classAndMethod = args[0].split("#");
if (helpCmd(args)) {
printHelpForTest(classAndMethod[1]);
System.exit(0);
}
Result result = runTest(classAndMethod);
exitIfTimeOutExceptionThrown(result);
System.exit(result.wasSuccessful() ? 0 : 1);
}
private static void exitIfTimeOutExceptionThrown(Result result) {
boolean isTimeOut = Iterables.filter(result.getFailures(), CodeBlockTimeOutException.class).iterator().hasNext();
if (isTimeOut)
{
System.exit(2);
}
}
运行此junit测试:
@Test
public void sendSearchRequest() throws Exception {
...
setTimeOut();
...
}
private void setTimeOut() {
if (criticalBlockTimeOut != null) {
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
throw new CodeBlockTimeOutException();
//thread.interrupt();
}
};
new Timer().schedule(timerTask, Long.parseLong(criticalBlockTimeOut));
}
}
为什么我看到我的代码抛出,CodeBlockTimeOutException
但是它的主代码永远不会退出2?
我如何将其扔到主线程中?
计时器事件发生在另一个线程中。主线程退出而没有错误。错误是从另一个日志记录的。如果出现异常,则应从计划的作业中设置一些易失性标志。等待作业在主线程中完成,然后检查此标志。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句