O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required