Package org.ojalgo.concurrent
Class ProcessingService
- java.lang.Object
-
- org.ojalgo.concurrent.ProcessingService
-
public final class ProcessingService extends java.lang.Object
A simple wrapper around anExecutorService
that makes it easier to process collections of items in parallel. The work items are processed by aConsumer
,Function
orTwoStepMapper
. In particular theTwoStepMapper
can 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 class
ProcessingService.CallableConsumer<W>
(package private) static class
ProcessingService.CallableMapper<W,R>
-
Field Summary
Fields Modifier and Type Field Description static ProcessingService
INSTANCE
private java.util.concurrent.ExecutorService
myExecutor
-
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.Divider
divider()
java.util.concurrent.ExecutorService
getExecutor()
<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 - aCollection
of input results in aCollection
of 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)
static ProcessingService
newInstance(java.lang.String name)
<W> void
process(java.util.Collection<? extends W> work, int parallelism, java.util.function.Consumer<W> processor)
Will create at mostparallelism
tasks to work through thework
items, processing them withprocessor
.<W> void
process(java.util.Collection<? extends W> work, java.util.function.Consumer<W> processor)
Using parallelismParallelism.CORES
.<W> void
process(java.util.Collection<? extends W> work, java.util.function.IntSupplier parallelism, java.util.function.Consumer<W> processor)
<W> void
processPair(W work1, W work2, java.util.function.Consumer<W> processor)
Just 2 work items.<W> void
processTriplet(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(Collection
instead,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(Collection
instead,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(Collection
instead,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 mostparallelism
tasks to work through thework
items, 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 mostparallelism
tasks to work through thework
items, 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
.void
run(int parallelism, java.lang.Runnable processor)
Will create preciselyparallelism
tasks that each execute theprocessor
.void
run(java.util.function.IntSupplier parallelism, java.lang.Runnable 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
parallelism
tasks to work through thework
items, processing them withcomputer
and 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
public DivideAndConquer.Divider divider()
-
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 - aCollection
of input results in aCollection
of 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)
-
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 mostparallelism
tasks to work through thework
items, 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(Collection
instead,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(Collection
instead,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(Collection
instead,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 mostparallelism
tasks to work through thework
items, processing them withreducer
. The state of each task'sreducer
will be combined into a single instance, and the results of that instance will be returned.Each
TwoStepMapper.Combineable
is 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.Combineable
implementation 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 mostparallelism
tasks to work through thework
items, processing them withreducer
. The results of each task'sreducer
will be merged into a single instance, and the results of that instance will be returned.Each
TwoStepMapper.Mergeable
is 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.Mergeable
implementation 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 preciselyparallelism
tasks 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)
-
-