Class ConcurrentRunner<T>

  • Type Parameters:
    T - The type of result for instances.

    public final class ConcurrentRunner<T>
    extends Object
    A class that will run code concurrently.
    Author:
    Rob Spoor
    Since:
    3.0
    • Method Detail

      • running

        public static <T> ConcurrentRunner<T> running​(ThrowingSupplier<? extends T> supplier)
        Creates a new concurrent runner.
        Type Parameters:
        T - The type of result.
        Parameters:
        supplier - The first supplier to call.
        Returns:
        The created concurrent runner.
        Throws:
        NullPointerException - If the given supplier is null.
      • running

        public static <T> ConcurrentRunner<T> running​(ThrowingSupplier<? extends T> supplier,
                                                      int count)
        Creates a new concurrent runner.
        Type Parameters:
        T - The type of result.
        Parameters:
        supplier - The first supplier to call.
        count - The number of times to call the supplier.
        Returns:
        The created concurrent runner.
        Throws:
        NullPointerException - If the given supplier is null.
        IllegalArgumentException - If the given count is not positive.
      • concurrentlyWith

        public ConcurrentRunner<T> concurrentlyWith​(ThrowingSupplier<? extends T> supplier)
        Adds a supplier to call concurrently.
        Parameters:
        supplier - The additional supplier to call.
        Returns:
        This object.
        Throws:
        NullPointerException - If the given supplier is null.
      • concurrentlyWith

        public ConcurrentRunner<T> concurrentlyWith​(ThrowingSupplier<? extends T> supplier,
                                                    int count)
        Adds a supplier to call concurrently.
        Parameters:
        supplier - The additional supplier to call.
        count - The number of times to call the supplier.
        Returns:
        This object.
        Throws:
        NullPointerException - If the given supplier is null.
        IllegalArgumentException - If the given count is not positive.
      • withThreadCount

        public ConcurrentRunner<T> withThreadCount​(int threadCount)
        Sets the number of threads to use. By default each provided supplier will get its own thread.
        Parameters:
        threadCount - The number of threads to use.
        Returns:
        This object.
        Throws:
        IllegalArgumentException - If the given thread count is not at least 2.
      • execute

        public ConcurrentResults<T> execute()
        Calls all provided suppliers concurrently using the provided number of threads. If no thread count has been given a thread for each provided supplier will be used.

        Note that calling this method will not process any result until a method on the return value is called.

        Returns:
        The results of calling the suppliers.
      • runConcurrently

        public static void runConcurrently​(Executable executable,
                                           int count)
        Runs a block of code several times concurrently. Each block of code will start at approximately the same time.

        This method assumes that each block of code does not throw any exception, including failed assertions. If any block of code does, this method will throw an error or exception.

        Parameters:
        executable - The block of code to run concurrently.
        count - The number of times to run the block of code.
        Throws:
        NullPointerException - If the given executable is null.
        IllegalArgumentException - If the given count is not positive.
      • runConcurrently

        public static void runConcurrently​(Executable executable,
                                           ConcurrencySettings settings)
        Runs a block of code several times concurrently. Each block of code will start at approximately the same time.

        This method assumes that each block of code does not throw any exception, including failed assertions. If any block of code does, this method will throw an error or exception.

        Parameters:
        executable - The block of code to run concurrently.
        settings - The concurrency settings, including the number of times to run the block of code.
        Throws:
        NullPointerException - If the given executable or settings object is null.
      • runConcurrently

        public static void runConcurrently​(Executable... executables)
        Runs several blocks of code concurrently. Each block of code will start at approximately the same time.

        This method assumes that each block of code does not throw any exception, including failed assertions. If any block of code does, this method will throw an error or exception.

        Parameters:
        executables - The blocks of code to run concurrently.
        Throws:
        NullPointerException - If any of the given executables is null.
      • runConcurrently

        public static void runConcurrently​(List<Executable> executables)
        Runs several blocks of code concurrently. Each block of code will start at approximately the same time.

        This method assumes that each block of code does not throw any exception, including failed assertions. If any block of code does, this method will throw an error or exception.

        Parameters:
        executables - The blocks of code to run concurrently.
        Throws:
        NullPointerException - If any of the given executables is null.