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.
Since Stream forEach doesn't have break, I think you can create Custom Exception for this to break the loop:
forEach
break
myDataStructure.stream() .forEach(e -> { if (System.currentTimeMillis() <= start + 30000) { throw new MyTimeOutException() } });
and you can catch this Exception for catch this.