Java asynchronously call a method for target output
A basic parallelStream will do exactly that:
boolean match = inputs.parallelStream().anyMatch(input -> check(input));
Returns early with match==true
, iff some input is found that matches check
.
match
will be false if all inputs are checked and none matched.
In the standard case it will use the fork/join thread pool. But with some further effort you can avoid that.
Here is a really simple working example to achieve what you are asking for
Future<Boolean> future = CompletableFuture.runAsync(() -> {
// Do your checks, if true, just return this future
System.out.println("I'll run in a separate thread than the main thread.");
});
// Now, you may want to block the main thread waiting for the result
while(!future.isDone()) {
// Meanwhile, you can do others stuff
System.out.println("Doing other stuff or simply waiting...");
}
// When future.isDone() returns, you will be able to retrieve the result
Boolean result = future.get();