- java.lang.Object
-
- org.ojalgo.concurrent.ProcessingService
-
public final class ProcessingService extends java.lang.ObjectA simple wrapper around anExecutorServicethat makes it easier to process collections of items in parallel. The work items are processed by aConsumer,FunctionorTwoStepMapper. In particular theTwoStepMappercan be used to aggregate/reduce data from the work items and then combine the collected data into a final result.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classProcessingService.CallableConsumer<W>(package private) static classProcessingService.CallableMapper<W,R>
-
Field Summary
Fields Modifier and Type Field Description static ProcessingServiceINSTANCEprivate java.util.concurrent.ExecutorServicemyExecutor
-
Constructor Summary
Constructors Constructor Description ProcessingService(java.util.concurrent.ExecutorService executor)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <W,R>
java.util.Map<W,R>compute(java.util.Collection<W> work, int parallelism, java.util.function.Function<W,R> computer)Compute an output item for each (unique) input item, and return the results as aMap.<W,R>
java.util.Map<W,R>compute(java.util.Collection<W> work, java.util.function.Function<W,R> computer)Using parallelismParallelism.CORES.<W,R>
java.util.Map<W,R>compute(java.util.Collection<W> work, java.util.function.IntSupplier parallelism, java.util.function.Function<W,R> computer)DivideAndConquer.Dividerdivider()Deprecated.v56 UsenewDivider()orParallelismSupplier.newDivider(int)insteadjava.util.concurrent.ExecutorServicegetExecutor()<W,R>
java.util.Collection<R>map(java.util.Collection<W> work, int parallelism, java.util.function.Function<W,R> mapper)Simply map each (unique) input item to an output item - aCollectionof input results in aCollectionof output.<W,R>
java.util.Collection<R>map(java.util.Collection<W> work, java.util.function.Function<W,R> mapper)Using parallelismParallelism.CORES.<W,R>
java.util.Collection<R>map(java.util.Collection<W> work, java.util.function.IntSupplier parallelism, java.util.function.Function<W,R> mapper)DivideAndConquer.DividernewDivider()static ProcessingServicenewInstance(java.lang.String name)<W> voidprocess(java.util.Collection<? extends W> work, int parallelism, java.util.function.Consumer<W> processor)Will create at mostparallelismtasks to work through theworkitems, processing them withprocessor.<W> voidprocess(java.util.Collection<? extends W> work, java.util.function.Consumer<W> processor)Using parallelismParallelism.CORES.<W> voidprocess(java.util.Collection<? extends W> work, java.util.function.IntSupplier parallelism, java.util.function.Consumer<W> processor)<W> voidprocessPair(W work1, W work2, java.util.function.Consumer<W> processor)Just 2 work items.<W> voidprocessTriplet(W work1, W work2, W work3, java.util.function.Consumer<W> processor)Just 3 work items.<W,R>
Rreduce(java.util.Collection<W> work, int parallelism, java.util.function.Supplier<? extends TwoStepMapper.Mergeable<W,R>> reducer)Deprecated.v54 Use#reduceMergeable(Collectioninstead,int,Supplier extends TwoStepMapper.Mergeable >) <W,R>
Rreduce(java.util.Collection<W> work, java.util.function.IntSupplier parallelism, java.util.function.Supplier<? extends TwoStepMapper.Mergeable<W,R>> reducer)Deprecated.v54 Use#reduceMergeable(Collectioninstead,IntSupplier,Supplier extends TwoStepMapper.Mergeable >) <W,R>
Rreduce(java.util.Collection<W> work, java.util.function.Supplier<? extends TwoStepMapper.Mergeable<W,R>> reducer)Deprecated.v54 Use#reduceMergeable(Collectioninstead,Supplier extends TwoStepMapper.Mergeable >) <W,R,A extends TwoStepMapper.Combineable<W,R,A>>
RreduceCombineable(java.util.Collection<W> work, int parallelism, java.util.function.Supplier<A> reducer)Will create at mostparallelismtasks to work through theworkitems, processing them withreducer.<W,R,A extends TwoStepMapper.Combineable<W,R,A>>
RreduceCombineable(java.util.Collection<W> work, java.util.function.IntSupplier parallelism, java.util.function.Supplier<A> reducer)<W,R,A extends TwoStepMapper.Combineable<W,R,A>>
RreduceCombineable(java.util.Collection<W> work, java.util.function.Supplier<A> reducer)Using parallelismParallelism.CORES.<W,R,A extends TwoStepMapper.Mergeable<W,R>>
RreduceMergeable(java.util.Collection<W> work, int parallelism, java.util.function.Supplier<A> reducer)Will create at mostparallelismtasks to work through theworkitems, processing them withreducer.<W,R,A extends TwoStepMapper.Mergeable<W,R>>
RreduceMergeable(java.util.Collection<W> work, java.util.function.IntSupplier parallelism, java.util.function.Supplier<A> reducer)<W,R,A extends TwoStepMapper.Mergeable<W,R>>
RreduceMergeable(java.util.Collection<W> work, java.util.function.Supplier<A> reducer)Using parallelismParallelism.CORES.voidrun(int parallelism, java.lang.Runnable processor)Will create preciselyparallelismtasks that each execute theprocessor.voidrun(java.lang.Runnable task1, java.lang.Runnable task2)voidrun(java.util.function.IntSupplier parallelism, java.lang.Runnable processor)<T> java.util.concurrent.atomic.AtomicBooleantake(java.util.concurrent.BlockingQueue<T> queue, int parallelism, java.util.function.Consumer<T> processor)Will submit preciselyparallelismtasks that each take from thequeuefeeding the items to theprocessor.<T> java.util.concurrent.atomic.AtomicBooleantake(java.util.concurrent.BlockingQueue<T> queue, java.util.function.IntSupplier parallelism, java.util.function.Consumer<T> processor)
-
-
-
Field Detail
-
INSTANCE
public static final ProcessingService INSTANCE
-
myExecutor
private final java.util.concurrent.ExecutorService myExecutor
-
-
Method Detail
-
newInstance
public static ProcessingService newInstance(java.lang.String name)
-
compute
public <W,R> java.util.Map<W,R> compute(java.util.Collection<W> work, java.util.function.Function<W,R> computer)Using parallelismParallelism.CORES.
-
compute
public <W,R> java.util.Map<W,R> compute(java.util.Collection<W> work, int parallelism, java.util.function.Function<W,R> computer)Compute an output item for each (unique) input item, and return the results as aMap. If the input contains duplicates, the output will have fewer items. It is therefore vital that the input type implementsObject.hashCode()andObject.equals(Object)properly.Will create at most
parallelismtasks to work through theworkitems, processing them withcomputerand collectiing the results in aMap.- Type Parameters:
W- The work item typeR- The function return type- Parameters:
work- The collection of work itemsparallelism- The maximum number of concurrent workers that will process the work itemscomputer- The processing code- Returns:
- A map of function input to output
-
compute
public <W,R> java.util.Map<W,R> compute(java.util.Collection<W> work, java.util.function.IntSupplier parallelism, java.util.function.Function<W,R> computer)- See Also:
compute(Collection, int, Function)
-
divider
@Deprecated public DivideAndConquer.Divider divider()
Deprecated.v56 UsenewDivider()orParallelismSupplier.newDivider(int)instead
-
getExecutor
public java.util.concurrent.ExecutorService getExecutor()
- Returns:
- The underlying
ExecutorService
-
map
public <W,R> java.util.Collection<R> map(java.util.Collection<W> work, java.util.function.Function<W,R> mapper)Using parallelismParallelism.CORES.- See Also:
map(Collection, IntSupplier, Function)
-
map
public <W,R> java.util.Collection<R> map(java.util.Collection<W> work, int parallelism, java.util.function.Function<W,R> mapper)Simply map each (unique) input item to an output item - aCollectionof input results in aCollectionof output. If the input contains duplicates, the output will have fewer items. It is therefore vital that the input type implementsObject.hashCode()andObject.equals(Object)properly.- Type Parameters:
W- The input item typeR- The output item type- Parameters:
work- The collection of work itemsparallelism- The maximum number of concurrent workers that will process the work itemsmapper- The mapper functiom- Returns:
- The mapped results
-
map
public <W,R> java.util.Collection<R> map(java.util.Collection<W> work, java.util.function.IntSupplier parallelism, java.util.function.Function<W,R> mapper)- See Also:
map(Collection, int, Function)
-
newDivider
public DivideAndConquer.Divider newDivider()
-
process
public <W> void process(java.util.Collection<? extends W> work, java.util.function.Consumer<W> processor)Using parallelismParallelism.CORES.
-
process
public <W> void process(java.util.Collection<? extends W> work, int parallelism, java.util.function.Consumer<W> processor)Will create at mostparallelismtasks to work through theworkitems, processing them withprocessor.- Type Parameters:
W- The work item type- Parameters:
work- The collection of work itemsparallelism- The maximum number of concurrent workers that will process the work itemsprocessor- The processing code
-
process
public <W> void process(java.util.Collection<? extends W> work, java.util.function.IntSupplier parallelism, java.util.function.Consumer<W> processor)- See Also:
process(Collection, int, Consumer)
-
processPair
public <W> void processPair(W work1, W work2, java.util.function.Consumer<W> processor)Just 2 work items.- See Also:
process(Collection, Consumer)
-
processTriplet
public <W> void processTriplet(W work1, W work2, W work3, java.util.function.Consumer<W> processor)Just 3 work items.- See Also:
process(Collection, Consumer)
-
reduce
@Deprecated public <W,R> R reduce(java.util.Collection<W> work, int parallelism, java.util.function.Supplier<? extends TwoStepMapper.Mergeable<W,R>> reducer)Deprecated.v54 Use#reduceMergeable(Collectioninstead,int,Supplier extends TwoStepMapper.Mergeable >)
-
reduce
@Deprecated public <W,R> R reduce(java.util.Collection<W> work, java.util.function.IntSupplier parallelism, java.util.function.Supplier<? extends TwoStepMapper.Mergeable<W,R>> reducer)Deprecated.v54 Use#reduceMergeable(Collectioninstead,IntSupplier,Supplier extends TwoStepMapper.Mergeable >)
-
reduce
@Deprecated public <W,R> R reduce(java.util.Collection<W> work, java.util.function.Supplier<? extends TwoStepMapper.Mergeable<W,R>> reducer)Deprecated.v54 Use#reduceMergeable(Collectioninstead,Supplier extends TwoStepMapper.Mergeable >)
-
reduceCombineable
public <W,R,A extends TwoStepMapper.Combineable<W,R,A>> R reduceCombineable(java.util.Collection<W> work, int parallelism, java.util.function.Supplier<A> reducer)
Will create at mostparallelismtasks to work through theworkitems, processing them withreducer. The state of each task'sreducerwill be combined into a single instance, and the results of that instance will be returned.Each
TwoStepMapper.Combineableis only worked on by a single thread, and the results are combined into a single instance. The instances are not reused.- Parameters:
work- The collection of work itemsparallelism- The maximum number of concurrent workers that will process the work itemsreducer- ATwoStepMapper.Combineableimplementation that does what you want.- Returns:
- The results...
-
reduceCombineable
public <W,R,A extends TwoStepMapper.Combineable<W,R,A>> R reduceCombineable(java.util.Collection<W> work, java.util.function.IntSupplier parallelism, java.util.function.Supplier<A> reducer)
-
reduceCombineable
public <W,R,A extends TwoStepMapper.Combineable<W,R,A>> R reduceCombineable(java.util.Collection<W> work, java.util.function.Supplier<A> reducer)
Using parallelismParallelism.CORES.
-
reduceMergeable
public <W,R,A extends TwoStepMapper.Mergeable<W,R>> R reduceMergeable(java.util.Collection<W> work, int parallelism, java.util.function.Supplier<A> reducer)
Will create at mostparallelismtasks to work through theworkitems, processing them withreducer. The results of each task'sreducerwill be merged into a single instance, and the results of that instance will be returned.Each
TwoStepMapper.Mergeableis only worked on by a single thread, and the results are combined into a single instance. The instances are not reused.- Parameters:
work- The collection of work itemsparallelism- The maximum number of concurrent workers that will process the work itemsreducer- ATwoStepMapper.Mergeableimplementation that does what you want.- Returns:
- The results...
-
reduceMergeable
public <W,R,A extends TwoStepMapper.Mergeable<W,R>> R reduceMergeable(java.util.Collection<W> work, java.util.function.IntSupplier parallelism, java.util.function.Supplier<A> reducer)
-
reduceMergeable
public <W,R,A extends TwoStepMapper.Mergeable<W,R>> R reduceMergeable(java.util.Collection<W> work, java.util.function.Supplier<A> reducer)
Using parallelismParallelism.CORES.
-
run
public void run(int parallelism, java.lang.Runnable processor)Will create preciselyparallelismtasks that each execute theprocessor.- Parameters:
parallelism- The number of concurrent workers/threads that will runprocessor- The processing code
-
run
public void run(java.util.function.IntSupplier parallelism, java.lang.Runnable processor)- See Also:
run(int, Runnable)
-
run
public void run(java.lang.Runnable task1, java.lang.Runnable task2)
-
take
public <T> java.util.concurrent.atomic.AtomicBoolean take(java.util.concurrent.BlockingQueue<T> queue, int parallelism, java.util.function.Consumer<T> processor)Will submit preciselyparallelismtasks that each take from thequeuefeeding the items to theprocessor. The tasks will continue to run until the returnedAtomicBooleanis set tofalse(or the thread is interrupted).If the threads of the underlying
ExecutorServiceare daemon threads, the JVM will not wait for them to finish before it exits. The default behaviour, usingINSTANCEornewInstance(String), is to make use of ojAlgo'sDaemonPoolExecutor.- Type Parameters:
T- The work item type- Parameters:
queue- The queue to take fromparallelism- How many parallel workers to createprocessor- What to do with each of the work items- Returns:
- A flag that can be used to signal the tasks to stop
-
take
public <T> java.util.concurrent.atomic.AtomicBoolean take(java.util.concurrent.BlockingQueue<T> queue, java.util.function.IntSupplier parallelism, java.util.function.Consumer<T> processor)- See Also:
take(BlockingQueue, int, Consumer)
-
-