Class ForEachInThreads<X>

  • Type Parameters:
    X - The type to iterate over
    All Implemented Interfaces:
    Proc<Iterable<X>>

    public final class ForEachInThreads<X>
    extends Object
    implements Proc<Iterable<X>>
    Executes a Proc in a new Thread for each element of an Iterable

    This class can be effectively used to iterate through a collection, just like Stream.forEach(java.util.function.Consumer) works, but with no guarantee on the output sorting:

    new ForEachInThreads( new ProcOf<>(input -> System.out.printf("\'%s\' ", input)), ).execute( new IterableOf<>("Mary", "John", "William", "Napkin") ); // Will print 'Mary' 'John' 'William' 'Napkin' to standard output. // Order of printing can be random.

    There is no thread-safety guarantee.

    Since:
    1.0
    • Constructor Detail

      • ForEachInThreads

        public ForEachInThreads​(Proc<X> proc)
        Ctor.
        Parameters:
        proc - The proc to execute
    • Method Detail

      • exec

        public void exec​(Iterable<X> input)
                  throws Exception
        Description copied from interface: Proc
        Execute it.
        Specified by:
        exec in interface Proc<X>
        Parameters:
        input - The argument
        Throws:
        Exception - If fails