Skip to content

Instantly share code, notes, and snippets.

@dborovikov
Created February 10, 2016 10:38
Show Gist options
  • Select an option

  • Save dborovikov/956a890bdd3094937678 to your computer and use it in GitHub Desktop.

Select an option

Save dborovikov/956a890bdd3094937678 to your computer and use it in GitHub Desktop.
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ParallelGroups {
public static final int CONCURRENCY = 5;
private static double[] compute(double[] a, double[] w) {
return a;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
final ExecutorService executor = Executors.newCachedThreadPool();
final List<double[]> input = new ArrayList<>();
final double[] w = new double[0];
for (List<double[]> group : Lists.partition(input, CONCURRENCY)) {
List<Future<double[]>> futures = Lists.transform(group, elem -> executor.submit(() -> compute(elem, w)));
for (Future<double[]> future : futures) {
double[] result = future.get();
for (int i = 0; i < result.length; i++) {
w[i] += result[i];
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment