// Path: src/main/java/com/cinchapi/common/base/Array.java
// public final class Array {
// 
//     /**
//      * Return an array containing all the {@code args}.
//      * <p>
//      * This method is just syntactic sugar for easily passing array objects to
//      * methods that don't take varargs.
//      * </p>
//      * 
//      * @param args the elements that should go in the array.
//      * @return args
//      */
//     @SafeVarargs
//     public static <T> T[] containing(T... args) {
//         return args;
//     }
// 
//     /**
//      * Return a new array that contains the items in the {@code args} array in
//      * reverse order.
//      * 
//      * @param args
//      * @return an array containing the {@code args} in reverse
//      */
//     @SafeVarargs
//     public static <T> T[] reverse(T... args) {
//         T[] copy = Arrays.copyOf(args, args.length);
//         reverseInPlace(copy);
//         return copy;
//     }
// 
//     /**
//      * Reverse the order of the items in the {@code args} array in-place (e.g.
//      * edit the input array).
//      * 
//      * @param args
//      */
//     @SafeVarargs
//     public static <T> void reverseInPlace(T... args) {
//         for (int i = 0; i < args.length / 2; ++i) {
//             T swap = args[i];
//             args[i] = args[args.length - i - 1];
//             args[args.length - i - 1] = swap;
//         }
//     }
// 
//     private Array() {}
// 
// }


import com.google.common.base.Preconditions;
import java.util.Arrays;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import com.cinchapi.common.base.Array;

    /**
     * Return the {@link Future} for the first of the {@code headStartTask} and
     * {@code tasks} to complete; giving the first {@code task}
     * {@code headStartTime} {@code headStartTimeUnit} to complete before
     * allowing the remaining {@code tasks} to race.
     * <p>
     * Even if the {@code headStartTask} does not finish within the head start
     * period, it may still finish before the other {@code tasks} and returns
     * its {@link Future}.
     * </p>
     * <p>
     * The {@link Future} is guaranteed to be {@link Future#isDone()} when this
     * method returns.
     * </p>
     * 
     * @param headStartTime
     * @param headStartTimeUnit
     * @param headStartTask
     * @param tasks
     * @return the {@link Future} of the completed task
     * @throws InterruptedException
     */
    public Future<V> raceWithHeadStart(long headStartTime,
            TimeUnit headStartTimeUnit, Runnable headStartTask,
            Runnable... tasks) throws InterruptedException {
        return raceWithHeadStart(headStartTime, headStartTimeUnit,
                Executors.callable(headStartTask, null),
                Arrays.stream(tasks).map(Executors::callable)
                        .collect(Collectors.toList())
// Path: src/main/java/com/cinchapi/common/base/Array.java
// public final class Array {
// 
//     /**
//      * Return an array containing all the {@code args}.
//      * <p>
//      * This method is just syntactic sugar for easily passing array objects to
//      * methods that don't take varargs.
//      * </p>
//      * 
//      * @param args the elements that should go in the array.
//      * @return args
//      */
//     @SafeVarargs
//     public static <T> T[] containing(T... args) {
//         return args;
//     }
// 
//     /**
//      * Return a new array that contains the items in the {@code args} array in
//      * reverse order.
//      * 
//      * @param args
//      * @return an array containing the {@code args} in reverse
//      */
//     @SafeVarargs
//     public static <T> T[] reverse(T... args) {
//         T[] copy = Arrays.copyOf(args, args.length);
//         reverseInPlace(copy);
//         return copy;
//     }
// 
//     /**
//      * Reverse the order of the items in the {@code args} array in-place (e.g.
//      * edit the input array).
//      * 
//      * @param args
//      */
//     @SafeVarargs
//     public static <T> void reverseInPlace(T... args) {
//         for (int i = 0; i < args.length / 2; ++i) {
//             T swap = args[i];
//             args[i] = args[args.length - i - 1];
//             args[args.length - i - 1] = swap;
//         }
//     }
// 
//     private Array() {}
// 
// }


// Path: src/main/java/com/cinchapi/common/concurrent/ExecutorRaceService.java
import com.google.common.base.Preconditions;
import java.util.Arrays;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import com.cinchapi.common.base.Array;


    /**
     * Return the {@link Future} for the first of the {@code headStartTask} and
     * {@code tasks} to complete; giving the first {@code task}
     * {@code headStartTime} {@code headStartTimeUnit} to complete before
     * allowing the remaining {@code tasks} to race.
     * <p>
     * Even if the {@code headStartTask} does not finish within the head start
     * period, it may still finish before the other {@code tasks} and returns
     * its {@link Future}.
     * </p>
     * <p>
     * The {@link Future} is guaranteed to be {@link Future#isDone()} when this
     * method returns.
     * </p>
     * 
     * @param headStartTime
     * @param headStartTimeUnit
     * @param headStartTask
     * @param tasks
     * @return the {@link Future} of the completed task
     * @throws InterruptedException
     */
    public Future<V> raceWithHeadStart(long headStartTime,
            TimeUnit headStartTimeUnit, Runnable headStartTask,
            Runnable... tasks) throws InterruptedException {
        return raceWithHeadStart(headStartTime, headStartTimeUnit,
                Executors.callable(headStartTask, null),
                Arrays.stream(tasks).map(Executors::callable)
                        .collect(Collectors.toList())
                        .toArray(Array.containing()));