出现异常的位置在After方法中遍历集合的那行
//在测试单元中有如下代码
//遍历Future载体
Thread thread=new Thread(()->{
listFuture.forEach(x->{
try {
if (x.isDone()) {
System.out.println("future:"+x+",result:"+x.get());
}
list.add(Long.toString(System.currentTimeMillis()));
} catch (Exception e) {
System.out.println("抛出异常:"+e);
}
});
});
thread.start();
//异常出现的位置在After单元中遍历集合一行
@After
public void clear(){
System.out.println("遍历list集合");
list.forEach(System.out::println);
}
官方说明:
问题出在遍历集合时thread线程并未执行完
解决方案:
保证子线程在主线程结束之前执行完毕。
重新测试后能用