I have code that should execute reasonably fast but occasionally may take long time to execute and produce a result. I'd like to limit the duration of that operation and abort it if it runs longer than a given time limit. I'd like the code to look like
Supplier<T> longRunningFoo = () -> {...}; // this may take a while to run
LongOpRunner runner = new LongOpRunner(longRunningFoo); // <-- some wrapper that limits operation duration
try {
T result = runner.call(10000 /* ms */); // abort after 10000 milliseconds
} catch (LongOpTimeout e) {
// handle timeout exception when "foo" gets aborted
}
Before I start writing my own I am interested to see if there are existing libraries that provide this capability.