我想循环一个巨大的数组并执行一组需要很长时间的复杂指令。但是,如果超过 30 秒,我希望它放弃。
ex.
final long start = System.currentTimeMillis();
myDataStructure.stream()
.while(() -> System.currentTimeMillis() <= start + 30000)
.forEach(e ->
{
...
});
我想避免只是说return
在 - 的里面forEach
如果满足特定条件则调用。
我会为此创建一个自定义池,例如:
ForkJoinPool forkJoinPool = new ForkJoinPool(1);
try {
forkJoinPool.submit(() ->
IntStream.range(1, 1_000_000).filter(x -> x > 2).boxed().collect(Collectors.toList()))
.get(30, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
// job not done in your interval
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)