Running multiple tasks and processing the first result

A common problem in concurrent programming arises when you have various concurrent tasks available to solve a problem, but you are only interested in the first result. For example, you want to sort an array. You have various sort algorithms. You can launch all of them and get the result of the first one that sorts the array, that is, the fastest sorting algorithm for a given array.

In this recipe, you will learn how to implement this scenario using the ThreadPoolExecutor class. You are going to use two mechanisms to try and validate a user. The user will be validated if one of these mechanisms is able to validate it.

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.