How it works...

The key of the example is in the Main class. The invokeAny() method of the ThreadPoolExecutor class receives a list of tasks, then launches them, and returns the result of the first task that finishes without throwing an exception. This method returns the same data type that the call() method of the tasks returns. In this case, it returned a String value.

The following screenshot shows the output of an execution of the example when one of the tasks validates the user:

The example has two UserValidator objects that return a random Boolean value. Each UserValidator object is used by a Callable object, implemented by the TaskValidator ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.