I want to loop over a huge array and do a complicated set of instructions that takes a long time. However, if more than 30 seconds have passed, I want it to give up.
ex.
I would create a custom pool for that, something like:
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
}