/* package whatever; // don't place package name! */

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.LockSupport;
import java.util.function.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
        String s=anyOf(Arrays.asList(
            CompletableFuture.supplyAsync(()->{throw new RuntimeException();}),
            CompletableFuture.supplyAsync(()->{ LockSupport.parkNanos(10_000_000_000L); return "hello"; })))
        .join();
        System.out.println(s);
    }
    public static <T>
        CompletableFuture<T> anyOf(List<? extends CompletionStage<? extends T>> l) {

        CompletableFuture<T> f=new CompletableFuture<>();
        Consumer<T> complete=f::complete;
        CompletableFuture.allOf(
            l.stream().map(s -> s.thenAccept(complete)).toArray(CompletableFuture<?>[]::new)
        ).exceptionally(ex -> { f.completeExceptionally(ex); return null; });
        return f;
	}
}