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>
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 controlled 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.
-
Nested Class Summary
Nested classes/interfaces inherited from interface one.microstream.functional.ParallelProcedure
ParallelProcedure.Default<E>, ParallelProcedure.LogicProvider<S,P extends Consumer<? super S>>, ParallelProcedure.ThreadCountProvider, ParallelProcedure.ThreadTimeoutProvider
-
Constructor Summary
Constructors Constructor Description Default(ParallelProcedure.LogicProvider<? super E,? extends Consumer<? super E>> logicProvider, int threadCount)
Default(ParallelProcedure.LogicProvider<? super E,? extends Consumer<? super E>> logicProvider, int threadCount, int threadTimeout)
Default(ParallelProcedure.LogicProvider<? super E,? extends Consumer<? super E>> logicProvider, ParallelProcedure.ThreadCountProvider threadCountProvider)
Alias forDefault(logicProvider, maxThreadCount, 1000)
.Default(ParallelProcedure.LogicProvider<? super E,? extends Consumer<? super E>> logicProvider, ParallelProcedure.ThreadCountProvider threadCountProvider, ParallelProcedure.ThreadTimeoutProvider threadTimeout)
Validates the passed parameters but does neither create threads nor call any of the logic provider's methods. -
Method Summary
Modifier and Type Method Description void
accept(E element)
Applies a wrapped logic by delegating its execution to another thread.int
currentThreadCount()
-
Constructor Details
-
Default
public Default(ParallelProcedure.LogicProvider<? super E,? extends Consumer<? super E>> logicProvider, int threadCount) -
Default
public Default(ParallelProcedure.LogicProvider<? super E,? extends Consumer<? super E>> logicProvider, int threadCount, int threadTimeout) -
Default
public Default(ParallelProcedure.LogicProvider<? super E,? extends Consumer<? super E>> logicProvider, ParallelProcedure.ThreadCountProvider threadCountProvider)Alias forDefault(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)Validates the passed parameters but does neither create threads nor call any of the logic provider's methods.Valid arguments are:
- non-null logic provider instance
- positive max thread count (greater than 0)
- positive thread timeout (greater than 0) in milliseconds
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 Details
-
currentThreadCount
public int currentThreadCount() -
accept
Description copied from interface:ParallelProcedure
Applies a wrapped logic by delegating its execution to another thread.
-