recursive-tasker's People
recursive-tasker's Issues
Simplificando
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
import java.util.function.Consumer;
public class RecursiveTasker<T> extends RecursiveTask<Collection<T>> {
private final List<T> tasks;
private final int start;
private final int end;
private final Consumer<T> itemAction;
private ForkJoinPool pool;
private int splitValue;
public RecursiveTasker(Collection<T> tasks, Consumer<T> itemAction) {
this(tasks, 0, tasks.size(), itemAction, getSplitValue(tasks), null);
}
public RecursiveTasker(Collection<T> tasks, Consumer<T> itemAction, ForkJoinPool pool) {
this(tasks, 0, tasks.size(), itemAction, getSplitValue(tasks), pool);
}
private RecursiveTasker(Collection<T> tasks, int start, int end, Consumer<T> itemAction, Integer splitValue, ForkJoinPool pool) {
this.pool = pool;
this.tasks = new ArrayList<>(tasks);
this.start = start;
this.end = end;
this.itemAction = itemAction;
this.splitValue = splitValue;
}
private static <T> int getSplitValue(Collection<T> tasks) {
return tasks.size() / (Runtime.getRuntime().availableProcessors() * 2);
}
@Override
protected Collection<T> compute() {
if (end - start <= splitValue) {
for (int i = start; i < end; i++) {
try {
itemAction.accept(tasks.get(i));
} catch (Exception e) {
pool.shutdownNow();
throw e;
}
}
return tasks.subList(start, end);
} else {
int middle = start + ((end - start) / 2);
RecursiveTask<Collection<T>> otherTask = new RecursiveTasker<>(tasks, start, middle, itemAction, splitValue, pool);
otherTask.fork();
Collection<T> resultList = new RecursiveTasker<>(tasks, middle, end, itemAction, splitValue, pool).compute();
resultList.addAll(otherTask.join());
return resultList;
}
}
public Collection<T> start(int splitValue) {
this.splitValue = splitValue;
return start();
}
public Collection<T> start() {
if (pool == null) {
pool = new ForkJoinPool();
}
return pool.invoke(this);
}
}
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.