Class ParallelProcedure.Default<E>

  • All Implemented Interfaces:
    Consumer<E>, ParallelProcedure<E>
    Enclosing interface:
    ParallelProcedure<E>

    public static final class ParallelProcedure.Default<E>
    extends Object
    implements ParallelProcedure<E>
    Sample implementation with a user defined number of maximum worker threads and a user defined timeout (in ms) after which one worker thread will be abolished.

    Worker threads will be created or abolished in line with the number of elements to be handled, the specified maximum thread count and the specified thread timeout.
    Thread abolishment is controled via calls of Thread.interrupt() from the thread management logic. Also, any other Throwable thrown by the logic Consumer will cause the throwing worker thread to be abolished as well and the logic instance to be disposed.

    Locking is done via the instance itself. There are opinions that say it is preferable to do locking via a dedicated internal unshared locking object to prevent "lock stealing" from outside. However, obtaining the particular lock in an outside context can also be desired or even needed. E.g.: synchronize on the procedure instance itself in the calling context, apply the procedure to all elements in a collection and then release the lock (leave the synchronized block) to start all worker threads as soon all the collection's elements have been collected. It is probably due to requirements like this that lock hiding strategies get extended by means to publicly present their hidden lock instance. But then that defeats the purpose of lock hiding altogether and yields the direct and simpler syntactical way of using this-synchronized internal methods the superior one. In the end, synchronizing/locking on the instance itself is fine. It just may not be messed up. As with anything else in writing code.

    • Constructor Detail

      • Default

        public Default​(ParallelProcedure.LogicProvider<? super E,​? extends Consumer<? super E>> logicProvider,
                       ParallelProcedure.ThreadCountProvider threadCountProvider)
        Alias for Default(logicProvider, maxThreadCount, 1000).
        Parameters:
        logicProvider - the instance that provides the logic instances to be used by the worker threads.
        threadCountProvider - the maximum number of concurrent threads to be created by this instance.
      • Default

        public Default​(ParallelProcedure.LogicProvider<? super E,​? extends Consumer<? super E>> logicProvider,
                       ParallelProcedure.ThreadCountProvider threadCountProvider,
                       ParallelProcedure.ThreadTimeoutProvider threadTimeout)
        Validetes the passed parameters but does neither create threads nor call any of the logic provider's methods.

        Valied arguments are:

        • non-null logic provider instance
        • positive max thread count (greater than 0)
        • positive thread timeout (greater than 0) in milliseconds
        Note that a low thread timeout can cause high overhead of frequent thread creation and destruction. A value of at least 100 or higher is advised.
        Also note that a thread count of 1 defeats the purpose of parallelism and that a very high thread count (depending on the system, like 10 to 100) will cause unnecessary thread management overhead in most cases.
        Parameters:
        logicProvider - the instance that provides the logic instances to be used by the worker threads.
        threadCountProvider - the maximum number of concurrent threads to be created by this instance.
        threadTimeout - the thread abolishment timeout in milliseconds.
    • Method Detail

      • currentThreadCount

        public int currentThreadCount()
      • accept

        public final void accept​(E element)
        Applies a wrapped logic by delegating its execution to another thread.
        Specified by:
        accept in interface Consumer<E>
        Specified by:
        accept in interface ParallelProcedure<E>
        Parameters:
        element -